Data-driven testing in NUnit?

前端 未结 5 1757
情歌与酒
情歌与酒 2020-12-14 03:40

In MSTest you can do something like:

[TestMethod]
[DataSource(\"Microsoft.VisualStudio.TestTools.DataSource.CSV\", 
            \"testdata.csv\", \"testdata#         


        
5条回答
  •  攒了一身酷
    2020-12-14 03:56

    Here is another example very similar to Tim Abell's however not using a framework for the CSV reader and showing the specifics of the test. Note when you use the TestCaseAttribute the TestAttribute can be omitted.

            [TestCaseSource("GetDataFromCSV")]
        public void TestDataFromCSV(int num1,int num2,int num3)
        {
            Assert.AreEqual(num1 + num2 ,num3);
        }
    
        private IEnumerable GetDataFromCSV()
        {
            CsvReader reader = new CsvReader(path);
            while (reader.Next())
            {
                int column1 = int.Parse(reader[0]);
                int column2 = int.Parse(reader[1]);
                int column3 = int.Parse(reader[2]);
                yield return new int[] { column1, column2, column3 };
            }
        }
    
    
    public class CsvReader : IDisposable
    {
        private string path;
        private string[] currentData;
        private StreamReader reader;
    
        public CsvReader(string path)
        {
            if (!File.Exists(path)) throw new InvalidOperationException("path does not exist");
            this.path = path;
            Initialize();
        }
    
        private void Initialize()
        {
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            reader = new StreamReader(stream);
        }
    
        public bool Next()
        {
            string current = null;
            if ((current = reader.ReadLine()) == null) return false;
            currentData = current.Split(',');
            return true;
        }
    
        public string this[int index]
        {
            get { return currentData[index]; }
        }
    
    
        public void Dispose()
        {
            reader.Close();
        }
    }
    

    CSV Data:

    10,200,210 20,190,210 30,180,210 40,170,210 50,160,210 60,150,210 70,140,210 80,130,210 90,120,210 100,110,210

    Note: The 3rd column is a sum of the first two columns and this will be asserted in the unit test.

    Results:

    results

    Find below an alternative using TestCaseData objects and setting a return type (which off-course is mandatory)

            [TestCaseSource("GetDataFromCSV2")]
        public int TestDataFromCSV2(int num1, int num2)
        {
            return num1 + num2;
        }
    
        private IEnumerable GetDataFromCSV2()
        {
            CsvReader reader = new CsvReader(path);
            while (reader.Next())
            {
                int column1 = int.Parse(reader[0]);
                int column2 = int.Parse(reader[1]);
                int column3 = int.Parse(reader[2]);
                yield return new TestCaseData(column1, column2).Returns(column3);
            }
        }
    

提交回复
热议问题