Jak umieścić nową listę {1} W walizce testowej NUNIT?

Mam metodę:

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

Oto mój test przeciwko niemu, ale nie podoba się new List<int> {1} w 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);
    }

Daje mi błąd:

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

Czy muszę to robić tak:

    [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);
    }
Author: xaisoft, 2013-10-20

8 answers

Istnieje jedna możliwość użycia atrybutu TestCaseSource. Tutaj podaję test nie twierdzący z dwoma przypadkami, aby zobaczyć, jak to działa:

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

W każdym razie muszę wspomnieć, że nie jest to najbardziej oczywiste rozwiązanie i wolałbym schludnie zorganizowane oprawy ignorując fakt, że są bardziej wyraziste

Więcej informacji: https://github.com/nunit/docs/wiki/TestCaseSource-Attribute

 52
Author: Yurii Hohan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-01-25 15:32:55

Moje rozwiązanie jest prostsze, używam tylko params. Mam nadzieję, że to ci pomoże!

[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);
}
 21
Author: Jaider,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-26 02:08:45

Często używam ciągów i parsowania, ponieważ ładnie renderuje w testrunnerze. Próbka:

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

Wygląda tak w ReSharper ' s runner:

Tutaj wpisz opis obrazka

 8
Author: Johan Larsson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-04-16 05:54:48

Popraw kod dla @ Yurii Hohan odpowiedź:

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))]
Mam nadzieję, że to pomoże.
 6
Author: zquanghoangz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-10-11 07:08:42

Możesz użyć tego:

[TestCase(new []{1,2,3})]
public void Add_WithOneNumber_ReturnsNumber(int[] numbers)
 3
Author: jecaestevez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-14 20:37:55

Użyj tablicy jako parametru new [] {1, 2} dla testów i przekonwertuj ją na Listę wewnątrz metody testowej 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);
}
 1
Author: Blechdose,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-01-24 18:28:53

Nie można używać obiektów tylko stałych czasu kompilacji w atrybutach danych. Aby uniknąć używania refleksji, które uważam za niezwykle nieczytelne i wcale nie odpowiednie do testu, który ma formalnie opisywać zachowanie tak wyraźnie, jak to możliwe, oto, co robię: {]}

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

To jeszcze kilka linii, ale to tylko dlatego, że chcę mieć czysty wynik testu. Możesz równie łatwo umieścić je w jednym [teście] , jeśli szukasz czegoś bardziej zwięzłego.

 -1
Author: Timothy Gonzalez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-29 17:51:49

Zamiast tego utwórz listę wewnątrz metody, w następujący sposób:

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

    Assert.AreEqual(1, result);
}
 -3
Author: Karl Anderson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-20 16:28:49