Saturday, February 9, 2013

A Step-by-Step Beginner Tutorial on Writing Unit Tests NUnit with C#

In this tutorial, you will learn how to setup and run NUnit tests inside a project. There are many useful tutorials but in my opinion, they are just too complex and time consuming as a start-up guide. Authors tend to put their own models to test and it is time consuming to read and understand their code to begin with. Also most people have their own domain and do not have time reading other people's domain, right?

So let's start our brief tutorial right here to let you stand on your feet in no time!


1. Create your project

You need to consider that you can have any type of project, you like. Working with NUnit does not restrict you to create an additional Unit Test Project (like Visual Studio's Test Suite):



Now, you need to add NUnit DLLs. Right click on project references, Click on add references:




Find NUnit assembly, check it:




Add a package (folder) to your project and name it something relevant like "Tests":




Add a Test Class and by that I mean just a normal class. What you should do is to add the following line at the top of your class:
using NUnit.Framework;

Now, here comes the main part. In a NUnit test class, beside the actual tests, they are other objects  which have their own methods from other classes. Why? Because in real-world scenarios a unit is not as simple as predicting a sum of two integers. In each method to be tested, a mixture of object from other classes is involved. Since it is called "Unit Test", we do not care whether those other second hand objects are working correctly. We create mocks out of them, and make them to act as we expect them to be. You might ask now when do we test their functionality? The answer is of course when we Unit-Test them. Now if you are really good you also might ask what if something gets lost in between? Yeah, no worries! Integration Testing or Behavior-Design-Development (BDD) is the answer.


Here is a complete layout of your unit test class. You can read the name of the methods and the comments and after that I trust that you can implement your own ideas, instead of reading my mind:
namespace HelloWorldNUnit.Tests
{
    [TestFixture]
    class UnitTest
    {

        [TestFixtureSetUp]
        public void RunsOnceBeforeAll()
        {
            // 1. Runs Once Before All of The Following Methods
            // Declare Global Objects Which Are Global For Test Class, e.g. Mock Objects
        }
        
        
        [SetUp]
        public void RunOnceBeforeEachTest()
        {
            // 2. Runs Twice; Once Before Test Case 1 and Once Before Test Case 2
            // Declare Objects Which Are Shared Among Tests, e.g. Shared Mocks
        }

        [Test]
        public void TestCaseNumberOne()
        { 
            // 3. Runs Once; This is your Test!
        }

        [Test]
        public void TestCaseNumberTwo()
        {
            // 4. Runs Once; This is your Test!
        }

        
        [TearDown]
        public void RunOnceAfterEachTests()
        {
            // 4. Runs Twice; Once after Test Case 1 and Once After Test Case 2
            // Dispose Objects Used in Each Test which are no longer required
        }

        [TestFixtureTearDown]
        public void RunOnceAfterAll()
        {
            // 5. Runs Once After All of The Aformentioned Methods
            // Dispose all Mocks and Global Objects
        }
    }
    
}

Build you solution. Find NUnit Gui Application (Default location is C:\Program Files (x86)\NUnit 2.6.2\bin). Go to File, then select Open Project and browse your application's EXE file (Again the default will be ~your-project-path\project-name\bin\Debug):




Now you can see the two aformentioned tests. If you put some logic there, build and then select File and then Reload Project, You can click on Run button and see that if your tests are passing/failing:



Happy Testing!   

2 comments:

  1. In this i'm getting an error at [TestFixtureSetUp]

    ReplyDelete
  2. Short good basic HOWTO to get me up and running then looking for more. Thanks!

    ReplyDelete