Skip to content

NUnit test cases

Testing multiple scenarios through the same function can be a bit of a time sink. Writing out a method for each scenario and just changing the data passed into the function then checking the output against the expected result.

Steve Fenton showed me NUnit test cases the other day though which make running multiple permutations of data against a result set a breeze! They have been around a while I think, but I somehow missed that boat. See below for a simple example of how they work.

    [TestFixture]
    public class RoyaltyQuizTests
    {
        [TestCase("Male", 34, "Harry")]
        [TestCase("Female", 37, "Kate")]
        [TestCase("Female", 92, "Elizabeth II")]
        [TestCase("Male", 97, "Philip")]
        public void RoyaltyTest(string gender, int age, string expected)
        {
            var resultOfMethod = new WhoAmI().GetWindsor(gender, age);
            Assert.AreEqual(expected, resultOfMethod);
        }

The test above will check multiple combinations of inputs against the expected output provided, all in a single test. This is especially useful when testing functions that contain nested Boolean logic.

Published inC#Testing

Comments are closed.