Data-driving test in C# using NUnit testing framework

By: Slav Kurochkin

Sometimes there is a need for tests which we call data-driven, where the same function should be executed against different data set.

Most of the common testing frameworks give us the opportunity to abstract data and it makes the testing code more clean and easy to maintain. With NUnit you can specify required data on top of the method, and all you have to do is use the tag [TestCase], then provide the data in sequence inside the tag declaration, once it is done you can easy create multiple arguments defined in the method name and supply to the body while test case execution, here is an example:

        [Test]
        [TestCase("Input data 1a", "Input Data 1b")]
        [TestCase("Input data 2a", "Input Data 2b")]
        public void DueDate(string a, string b)
        {
            Console.WriteLine("Here is: " + a + " and also " + b)
        }
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.