How To Pass Parameters Into Your NUnit Tests

When a lot of people start unit testing, they seem to think that because the code isn't 'production' code that the unit test code doesn't really matter that much and that they can hack together something quick and dirty and it doesn't matter.

Generally, what happens when people take this approach, is that after a few months, maintaining and keeping the tests working becomes a nightmare and people either give up writing and maintaining the tests and consider writing unit tests as a waste of time, or, they have to spend several weeks sorting the code out.

My warning to you is that it's worth spending a little bit of effort up-front writing good code, as it will make your life much simpler and easier in the long run.

One big pain point around tests is duplicate code. Like all good code, you should follow the DRY prinicple when you write tests. If you refactor some code and then 15 different tests break, it's annoying having to go everywhere and fix it. If you can follow the DRY prinicple when you write your tests, maintenance will become a lot more pleasurable expereince.

One way of sticking to the DRY prinicple, is by passing in parameters to your NUnit tests instead of duplicating the tests, for example, imagine this test:

[Test]
public Test_Option_1()
{
     Assert.IsTrue(CallMethod(true), true);
}

public Test_Option_1()
{
     Assert.IsTrue(CallMethod(false), false);
}

This is a very basic example, but, if you had to update CallMethod() you have two tests to fix. When we use NUnit we can pass parameters into tests, so the above test can also be written like this:

        [Test]
        [TestCase(false)]
        [TestCase(true)]
        public void Test_Option(bool state)
        {
              Assert.IsTrue(CallMethod(state), state);
        }

This makes our unit test code a lot simpler. If you make a change, you only have to change one test instead of two. In more complex scenarios you may pass 5-10 parameters into a test. Following this approach will make your code base more robust, so I suggest you start using it now.

 

How To Pass Objects Into Unit Tests

The above code works well for the standard out of the box data-types, but if you try and instantiate a new object as an input parameter, Visual Studio will be quick to point out that you can't do that.  Fear not, NUNit provides another way to pass parameters into your tests, like so:

        public static IEnumerable InputParameterTestData
        {
            get
            {
                yield return new TestCaseData(new MyClass(1));
                yield return new TestCaseData(new MyClass(1));
            }
        }

        [Test]
        [TestCaseSource("InputParameterTestData")]
        public void TestayClass(
            MyClass myClass)
        {
            var result = CalLTest(myClass);
            result.Should().BeNull();
        }

NUnit Takeway

As you can see, passing in parameters into your tests is pretty easy and it will drastically reduce the amount of duplicate nunit test code that you'll need to write.

submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top