Data driven test in NUnit with csv source data

1 minute read (339 words)

I wanted to test a date parser across a large range of values so wanted a simple test harness to test all the values.

The test framework options around c# / .net seem to be:

  • MSTest - can do csv via jet, but can’t do inline test data which is something I also want.
  • NUnit - can do inline data driven test data (with the TestCase(data…) attribute), and has support for extending this via the TestCaseSource attribute.
  • xUnit - confusing (aka flexible), doesn’t seem to get me to my end result any faster after a bit of searching around.

I’ve used NUnit and combined TestCaseSource with a simple wrapper class around the csv parsing library

To get this to work:

  • Save your csv file in your test project
  • add the file to your project (in visual studio 2008 in this case)
  • right-click on the csv file in solution explorer, click properties, change β€œCopy to Output Directory” to β€œCopy Always”
  • download the binaries (dlls) for csv reader from code project, add a reference to this in your test project
  • add a private method to your test class for reading the csv file and returning an enumarable (see code below)
  • add the TestCaseSource attribute to your test method(s) that you want to use the csv data, referencing your new IEnumerable method (see code below)
using System.Collections.Generic;
using System.IO;
using LumenWorks.Framework.IO.Csv;
using NUnit.Framework;

namespace mytests
{
    class MegaTests
    {
        [Test, TestCaseSource("GetTestData")]
        public void MyExample_Test(int data1, int data2, int expectedOutput)
        {
            var methodOutput = MethodUnderTest(data2, data1);
            Assert.AreEqual(expectedOutput, methodOutput, string.Format("Method failed for data1: {0}, data2: {1}", data1, data2));
        }

        private int MethodUnderTest(int data2, int data1)
        {
            return 42; //todo: real implementation
        }

        private IEnumerable<int[]> GetTestData()
        {
            using (var csv = new CsvReader(new StreamReader("test-data.csv"), true))
            {
                while (csv.ReadNextRecord())
                {
                    int data1 = int.Parse(csv[0]);
                    int data2 = int.Parse(csv[1]);
                    int expectedOutput = int.Parse(csv[2]);
                    yield return new[] { data1, data2, expectedOutput };
                }
            }
        }
    }
}

references:


Tweet This || Post to LinkedIn || Page Source

Subscribe for updates on software development, contracting, side projects, blog posts and who knows what else. Read the archives for an idea of content.

Mailing list powered by the excellent buttondown.email.