46

我有方法:

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();
    }

这是我对它的测试,但它不喜欢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);
    }

它给了我错误:

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

我是否必须这样做:

    [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);
    }
4

8 回答 8

61

有一种使用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);
    }
}

无论如何我不得不提到它不是最明显的解决方案,我更喜欢整齐组织的装置,忽略它们更冗长的事实

更多信息: https ://github.com/nunit/docs/wiki/TestCaseSource-Attribute

于 2013-10-20T16:59:05.117 回答
27

我的解决方案更简单,我只使用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);
}
于 2015-12-08T21:14:50.533 回答
11

改进@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))]

希望这有帮助。

于 2018-10-11T07:08:42.413 回答
8

我经常使用字符串和解析,因为它在测试运行程序中呈现得很好。样本:

[TestCase("1, 2")]
[TestCase("1, 2, 3")]
public void WithStrings(string listString)
{
    var list = listString.Split(',')
                         .Select(int.Parse)
                         .ToList();
    ...
}

在 Resharper 的跑步者中看起来像这样:

在此处输入图像描述

于 2016-05-21T15:13:48.500 回答
4

你可以使用这个:

[TestCase(new []{1,2,3})]
public void Add_WithOneNumber_ReturnsNumber(int[] numbers)
于 2018-10-18T10:31:51.117 回答
3

使用数组作为new [] {1, 2}测试用例的参数,并将其转换为测试方法中的列表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);
}
于 2019-04-14T15:56:47.367 回答
-2

您不能在数据属性中仅使用对象的编译时常量。为了避免使用反射,我发现反射非常难以理解,并且根本不适合旨在尽可能清楚地正式描述行为的测试,这就是我所做的:

    [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);
    }

还有几行,但这只是因为我想要清晰的测试输出。如果您正在寻找更简洁的东西,您可以轻松地将它们放在一个 [Test] 中。

于 2016-12-29T17:51:49.290 回答
-4

只需在方法内创建列表,如下所示:

public void Add_WithOneNumber_ReturnsNumber()
{
    var result = CalculatorLibrary.CalculatorFunctions.Add(new List<int>{1});

    Assert.AreEqual(1, result);
}
于 2013-10-20T16:28:49.550 回答