How do I put new List {1} in an NUNIT TestCase?

后端 未结 8 1858
后悔当初
后悔当初 2020-12-13 13:12

I have the method:

public static int Add(List numbers)
    {
        if (numbers == null || numbers.Count == 0)
            return 0;

        if          


        
相关标签:
8条回答
  • 2020-12-13 13:20

    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 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

    0 讨论(0)
  • 2020-12-13 13:23

    use array as parameter new [] {1, 2} for the Testcases and convert it to List inside the test method 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);
    }
    
    0 讨论(0)
  • 2020-12-13 13:24

    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);
    }
    
    0 讨论(0)
  • 2020-12-13 13:27

    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.

    0 讨论(0)
  • 2020-12-13 13:37

    You can use this :

    [TestCase(new []{1,2,3})]
    public void Add_WithOneNumber_ReturnsNumber(int[] numbers)
    
    0 讨论(0)
  • 2020-12-13 13:41

    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:

    0 讨论(0)
提交回复
热议问题