2012-03-03 60 views
8

我有以下试验:传递单个值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(); 
} 

第一测试用例(显然)失败,错误如下:

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

我试图更换测试用例与这一个定义:

[TestCase(new[] { param1 })] 

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

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

我现在的解决方案是将'单参数'情况移到不同的测试方法。

还有,有没有办法让这个测试和其他测试一样?

回答

8

一种方法可能是使用TestCaseSource,并有一个方法返回每个参数集,而不是使用TestCase。

3

在回答这个问题“NUnit cannot recognize a TestCase when it contains an array”,编译错误从bug茎,并且可以使用语法来命名的测试用例来克服,因为这种基于this answer

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

您链接到ReSharper的问题,而问题似乎是在nunit ......在任何情况下,似乎resharper解决了这个问题 – Noctis 2014-10-03 05:35:02

+1

这样做与'new [] {...}'数组是字符串给出错误CS0182引用上面的OP。整数类型似乎正常工作。 – Brandon 2015-02-23 18:21:19