Parameterised Unit Tests

My resent project is using NUnit (a xUnit framework under .NET implementation) to do unit testing. And today, my colleague gave a brilliant suggestion on using NUnit to increase code coverage with Parameterised Unit Tests technique. Here’s the quote from his email.

TestCase attribute can be used to test multiple scenarios, or different code paths.

The following test method will result in four unit tests with four different input… increasing code coverage.

        [TestCase("anything")]  // search with keyword
        [TestCase("")]          // search with empty string
        [TestCase(null)]        // search with null
        public void Search_should_return_pagedList(string keyword)
            // Action
            var result = _searchService.Search<SolrOrganisation>(keyword);

            // Assert
            Assert.AreEqual(2, result.ToList().Count);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s