Introduction - NUnit Tutorial

C#  NUnit  C#  testing 

NUnit is most popular tool for doing Unit Testing. Before start, we need to learn what is Unit Testing and why NUnit is a popular tool for doing unit testing.

Unit Testing

Every software is composed of various modules. Each module is composed of various classes. Classes composed of various functions. Function is the smallest unit of code in the application.

When we test individual function behavior without touching any other functions and determine whether it works exactly as per the requirements or not that is called Unit Testing.

Some of the advantages of Unit Testing:

  1. Defects found early lin development life cycle
  2. Reliable Code
  3. Maintainable code
  4. Faster testing by only single click of action

NUnit

NUnit is a unit testing framework for .NET. It is the most used framework for writing unit test cases.

We can write testing code in either C# or VB.NET. It is suggested to write testing code in different assemblies called Test Assemblies. These assemblies only contain testing code nothing else. We need to run these test assemblies to check whether all test cases are passed or failed. For that we required Test Runner.

Test Runners are UI tool which actually run NUnit test cases and show the result of test cases whether they are passed or failed. We'll learn about test runners in Environment Setup in next post.

NUnit is very easy to use. It only provides some custom attributes and some static Assert classes. With the combination of custom attributes and static classes, we can write unit test cases easily.

Custom attributes provides hint to NUnit test runners that these classes or functions contains unit testing code. Assert classes is used to test the conditions whether system under test (SUT) satisfy a condition or not. If condition is satisfied then test is pass else fail.

Some of the custom attributes are:

  • TestFixture
  • Setup
  • TearDown
  • Test
  • Category
  • Ignore
  • TestCase
  • Repeat
  • MaxTime

In the next post, We'll learn how to setup your environment for writing NUnit test cases.

Join our newsletter and get an occasional email with a technology and DotNetPattern.com news update.