传递单个值PARAMS论点NUnit的测试用例 [英] Passing single value to params argument in NUnit TestCase

查看:197
本文介绍了传递单个值PARAMS论点NUnit的测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试:

[ExpectedException(typeof(ParametersParseException))]
[TestCase("param1")]
[TestCase("param1", "param2")]
[TestCase("param1", "param2", "param3", "optParam4", "optParam5", "some extra parameter")]
public void Parse_InvalidParametersNumber_ThrowsException(params string[] args)
{
    new ParametersParser(args).Parse();
}

第一个TestCase的(显然)失败,出现以下错误:

The first TestCase (obviously) fails with the following error:

System.ArgumentException : Object of type 'System.String' 
cannot be converted to type 'System.String[]'.



我试图用这个来代替TestCase的定义:

I tried to replace the TestCase definition with this one:

[TestCase(new[] { param1 })]

但现在我得到以下编译错误:

but now I get the following compilation error:

错误CS0182:属性参数必须是常量表达式,typeof运算表达式或一个属性参数类型

error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

我现在是'一个参数的情况下移动到不同的测试方法的解决方案。

My solution for now is moving the 'one parameter' case to a different test method.

不过,有没有办法让这个测试运行方式与其他人一样的吗?

Still, is there a way to get this test to run the same way as the others?

推荐答案

的一种方法是使用 TestCaseSource ,并有方法返回各自的参数组,而不是使用测试用例

One way could be to use TestCaseSource, and have a method that returns each parameter set, instead of using TestCase.

这篇关于传递单个值PARAMS论点NUnit的测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆