问题
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:
[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
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);
}
回答1:
There is one option to use TestCaseSource attribute. Here I provide a non-assert test with two cases just to see how it works:
[TestFixture]
public class TestClass
{
private object[] _sourceLists = {new object[] {new List<int> {1}}, //case 1
new object[] {new List<int> {1, 2}} //case 2
};
[Test, 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
回答2:
My solution is simpler, I just use params
. I hope this works for you!
[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);
}
回答3:
I often use strings and parsing as it renders nicely in the testrunner. Sample:
[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:
回答4:
Improve code for @Yurii Hohan answer:
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.
回答5:
You can use this :
[TestCase(new []{1,2,3})]
public void Add_WithOneNumber_ReturnsNumber(int[] numbers)
回答6:
use array as parameter new [] {1}
and convert it to List inside the test method result.ToList()
. This needs using System.Linq;
.
[TestCase(new [] {1}, 1)]
[TestCase(new [] {2}, 2)]
[TestCase(new [] {1000}, 1000)]
public void Add_WithOneNumber_ReturnsNumber(int[] numbers)
{
var result = CalculatorLibrary.CalculatorFunctions.Add(numbers);
Assert.AreEqual(1, result.ToList());
}
回答7:
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.
回答8:
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);
}
来源:https://stackoverflow.com/questions/19479817/how-do-i-put-new-listint-1-in-an-nunit-testcase