C# 如何将 new List<int> {1} 放入 NUNIT TestCase?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19479817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I put new List<int> {1} in an NUNIT TestCase?
提问by xaisoft
I have the method:
我有方法:
public static int Add(List<int> numbers)
{
if (numbers == null || numbers.Count == 0)
return 0;
if (numbers.Count == 1)
return numbers[0];
throw new NotImplementedException();
}
Here is my test against it, but it does not like new List<int> {1}
in the TestCase:
这是我针对它的测试,但它不喜欢new List<int> {1}
在 TestCase 中:
[TestCase(new List<int>{1}, 1)]
public void Add_WithOneNumber_ReturnsNumber(List<int> numbers)
{
var result = CalculatorLibrary.CalculatorFunctions.Add(numbers);
Assert.AreEqual(1, result);
}
It gives me the error:
它给了我错误:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
Do I have to do it like this:
我必须这样做吗:
[Test]
public void Add_WithOneNumber_ReturnsNumber()
{
var result = CalculatorLibrary.CalculatorFunctions.Add(new List<int>{7});
Assert.AreEqual(7, result);
var result2 = CalculatorLibrary.CalculatorFunctions.Add(new List<int> {3});
Assert.AreEqual(4,result2);
}
采纳答案by Yurii Hohan
There is one option to use TestCaseSourceattribute. Here I provide a non-assert test with two cases just to see how it works:
有一种使用TestCaseSource属性的选项。在这里,我提供了一个包含两种情况的非断言测试,只是为了看看它是如何工作的:
[TestFixture]
public class TestClass
{
private static readonly object[] _sourceLists =
{
new object[] {new List<int> {1}}, //case 1
new object[] {new List<int> {1, 2}} //case 2
};
[TestCaseSource("_sourceLists")]
public void Test(List<int> list)
{
foreach (var item in list)
Console.WriteLine(item);
}
}
Anyhow I have to mention it is not the most evident solution and I would prefer neatly organized fixtures ignoring the fact they are more verbose
无论如何,我不得不提到这不是最明显的解决方案,我更喜欢整齐组织的装置,而忽略它们更冗长的事实
More information: https://github.com/nunit/docs/wiki/TestCaseSource-Attribute
更多信息:https: //github.com/nunit/docs/wiki/TestCaseSource-Attribute
回答by Karl Anderson
Just create the list inside the method instead, like this:
只需在方法内创建列表,如下所示:
public void Add_WithOneNumber_ReturnsNumber()
{
var result = CalculatorLibrary.CalculatorFunctions.Add(new List<int>{1});
Assert.AreEqual(1, result);
}
回答by Jaider
My solution is simpler, I just use params
. I hope this works for you!
我的解决方案更简单,我只使用params
. 我希望这对你有用!
[TestCase(1, 1)]
[TestCase(10, 5, 1, 4)]
[TestCase(25, 3, 5, 5, 12)]
public void Linq_Add_ShouldSumAllTheNumbers(int expected, params int[] numbers)
{
var result = CalculatorLibrary.CalculatorFunctions.Add(numbers);
Assert.AreEqual(expected, result);
}
回答by Johan Larsson
I often use strings and parsing as it renders nicely in the testrunner. Sample:
我经常使用字符串和解析,因为它在 testrunner 中呈现得很好。样本:
[TestCase("1, 2")]
[TestCase("1, 2, 3")]
public void WithStrings(string listString)
{
var list = listString.Split(',')
.Select(int.Parse)
.ToList();
...
}
Looks like this in Resharper's runner:
在 Resharper 的跑步者中看起来像这样:
回答by Timothy Gonzalez
You can't use objects only compile-time constants in data attributes. To avoid using reflection, which I find to be extremely unreadable and not at all appropriate for a test which is meant to formally describe behavior as clearly as possible, here's what I do:
您不能在数据属性中仅使用对象作为编译时常量。为了避免使用反射,我发现它非常不可读,而且根本不适合旨在尽可能清楚地正式描述行为的测试,我这样做:
[Test]
public void Test_Case_One()
{
AssertCurrency(INPUT, EXPECTED);
}
[Test]
public void Test_Case_Two()
{
AssertCurrency(INPUT, EXPECTED);
}
private void AssertScenario(int input, int expected)
{
Assert.AreEqual(expected, input);
}
It's a few more lines, but that's only because I want clear test output. You could just as easily put them in one [Test] if you are looking for something more concise.
还有几行,但这只是因为我想要清晰的测试输出。如果您正在寻找更简洁的东西,您可以轻松地将它们放在一个 [测试] 中。
回答by zquanghoangz
Improve code for @Yurii Hohan answer:
改进@Yurii Hohan 答案的代码:
private static readonly object[] _Data =
{
new object[] {new List<int> {0}, "test"},
new object[] {new List<int> {0, 5}, "test this"},
};
[Test, TestCaseSource(nameof(_Data))]
Hope this help.
希望这有帮助。
回答by jecaestevez
You can use this :
你可以使用这个:
[TestCase(new []{1,2,3})]
public void Add_WithOneNumber_ReturnsNumber(int[] numbers)
回答by Blechdose
use array as parameter new [] {1, 2}
for the Testcases and convert it to List inside the test method numbers.ToList()
.
使用数组作为new [] {1, 2}
测试用例的参数,并在测试方法中将其转换为 List numbers.ToList()
。
using System.Linq
...
[TestCase(new [] {1}, 1)]
[TestCase(new [] {1, 2}, 3)]
[TestCase(new [] {1, 2, 3}, 6)]
public void Return_sum_of_numbers(int[] numbers, int expectedSum)
{
var sum = CalculatorLibrary.CalculatorFunctions.Add(numbers.ToList());
Assert.AreEqual(expectedSum, sum );
// much cooler with FluentAssertions nuget:
// sum.Should.Be(expectedSum);
}