简体   繁体   English

我如何放置新列表<int> {1} 在 NUNIT 测试用例中?

[英]How do I put new List<int> {1} in an NUNIT TestCase?

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}

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

There is one option to use TestCaseSource attribute.有一种使用TestCaseSource属性的选项。 Here I provide a non-assert test with two cases just to see how it works:在这里,我提供了一个包含两种情况的非断言测试,只是为了看看它是如何工作的:

[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

My solution is simpler, I just use params .我的解决方案更简单,我只使用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);
}

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.希望这有帮助。

I often use strings and parsing as it renders nicely in the testrunner.我经常使用字符串和解析,因为它在 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:在 Resharper 的跑步者中看起来像这样:

在此处输入图片说明

You can use this :你可以使用这个:

[TestCase(new []{1,2,3})]
public void Add_WithOneNumber_ReturnsNumber(int[] numbers)

use array as parameter new [] {1, 2} for the Testcases and convert it to List inside the test method numbers.ToList() .使用数组作为测试用例的参数new [] {1, 2}并在测试方法numbers.ToList()中将其转换为 List 。

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

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.如果您正在寻找更简洁的东西,您可以轻松地将它们放在一个 [测试] 中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM