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

后端 未结 8 1906
后悔当初
后悔当初 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 {1}},   //case 1
            new object[] {new List {1, 2}} //case 2
        };
    
        [TestCaseSource("_sourceLists")]
        public void Test(List 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

提交回复
热议问题