Translate

Thursday, November 17, 2016

c# Unit testing

As a programmer, it is very crucial to learn testing techniques that will make my programs better. A method of doing this is through a type of automated testing called Unit testing. Depending on the language it is called different things, such as JUnit for Java Unit testing and NUnit for C# Unit testing. It really is the same principle no matter what language you use. In this post I will go through the basics using NUnit as the example.
What is unit testing? According to google, the source of all knowledge, "Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. "
This means that the goal of unit testing to test each individual part independently of the other parts. On a side note it is usually a good idea to have these written before you start programming, that way you are not customizing a bias test when your function is done.
Now that we know what it is lets see how to create a good unit test.

Step 1: Creating the logic of the test
This is usually done by the Requirements specifications. In other words, what is this supposed to do. Other things to note is that you also want to make sure when something happens that is not supposed to, that you handle it accordingly (error messages and such).

Step 2: Create the test
In the IDE, Microsoft Visual Studio, it makes it very simple to write your unit tests and to run them.


In this .gif it shows how to add a test to a already created project. In other words, make your project then add your test class to it.






This screenshot shows an example of the test class and a unit test within that test class
In this you can see that there are three parts to a test arrange, act, and assert. Arrange is the set up of the test in other words the preconditions or the test parameters. Act is the action, method or function being tested is called upon to modify the preconditions. The assert is to make sure that you get what your supposed to. This test that is shown is testing for a right answer. There are tests, like the one below, that make sure errors are displayed given a certain precondition.

Step 3: Write Code
This means you write the programs code, the testing code should already be mostly done.

Step 4: Modify Tests
Requirements could have changed while you were writing certain functions and you must update your tests accordingly.

Step 5: Run the Tests
You should be doing this as you finish the functions. That way you know that the function is written correctly. Shown below is how you run an individual test.


Step 6: Run All Tests
When your code is completely done, you should be able to run all tests and they all pass.

These steps should allow you to get started on writing your own tests. This is in no way a comprehensive way of doing all unit tests, but you should be able to start. Also, depending on the language and IDE it could be slightly different.

2 comments :

  1. This isn't N-Unit testing. It's Microsoft testing.

    ReplyDelete
    Replies
    1. You are absolutely right. Nunit is a test library. Correct naming is Unit testing with MsTest

      Delete