Unit Testing With NUnit



Unit Testing is a methodology in computer programming where the smallest unit of software logic is tested to determine if it's correctly doing what the programmer has designed for it to do. Performing unit tests on the programs can effectively reduce and prevent bugs in the program and improve the coding efficiency of the developer.

Unit Testing requires skill and patience. At the start, performing unit tests may seem to just lengthen the time of coding and double your effort since you now need to write additional codes for testing. But in the long run, as you make myriads of changes to the software to cater to customer's unending demands, unit testing can help a lot as it can easily detect if a logic is broken due to the latest changes in your codes.

There are a lot of Frameworks in .NET that can help the developer perform unit testing easily. One that I like is NUnit. I've used NUnit in one of my projects and it's easy to understand and use. NUnit is an open source product so it's free to use. You can download it from http://www.nunit.org/.  When it's installed, it has a Test Runner program that can be used to automatically run your test.

To demonstrate the use of NUnit, I'm going to test the following simple class with its "IsNoteValid" method:

public class Notes
    {       
        public bool IsNoteValid(string note)
        {
            return !string.IsNullOrEmpty(note); 
        }
    }

"IsNoteValid" procedure returns false if its parameter is empty or null and returns true otherwise.

How do we test this code? A unit test usually comprises three main actions: ARRANGE, ACT, and ASSERT.
Arrange is the process of setting up the objects, Act is the processing of calling the procedure under test and Assert is the process of making sure the procedure under test behaves according to its specification. In our case, we are going to test the "IsNoteValid" procedure if it behaves correctly.

Below are the test codes. You can search on the internet for the proper syntax of NUnit but basically in the first Test Procedure, it is just calling the IsNoteValid procedure with a valid parameter and asserting that it must return true. In the second Test Procedure, it is calling the IsNoteValid procedure with an invalid parameter and asserting that it must return false.

       [TestFixture]
        public class NotesTests
        {
            [Test]
            public void IsNoteValid_validNote_ReturnsTrue()
            {
                //arrange
                Notes note = new Notes();
                //act
                bool result = note.IsNoteValid("mynote");
                //assert
                Assert.IsTrue(result, "note should be valid!");
            }

            [Test]
            public void IsNoteValid_invalidNote_ReturnsFalse()
            {
                //arrange
                Notes note = new Notes();
                //act
                bool result = note.IsNoteValid(string.Empty);
                //assert
                Assert.IsFalse(result, "note should be invalid!");
            }
        }

When the Test codes are done, you can run Test Runner, select the assembly .dll of your Test Project and click  Run. The Test Runner  will display how many tests have passed and failed.



So when at a later time, I need to refactor and modify my Notes codes, all I have to do is Run the Test Runner again to see if it has failed a test. This way, bugs can easily be detected.




0 comments:

Post a Comment