Sunday, March 8, 2020

Implementation of NUnit Testing Framework

What is Unit Testing?

In this IT world a unit refers to simply a smallest piece of code which takes an input ,does certain operation, and gives an output. And testing this small piece of code is called Unit Testing.


A lot of unit test frameworks are available for .Net nowadays, if we check in Visual Studio we have MS-Test from Microsoft integrated in Visual Studio. Some 3rd party frameworks are:

  • NUnit
  • MbUnit
What is NUnit Testing?

NUnit is a unit-testing framework for .NET applications in which the entire application is isolated into diverse modules. Each module is tested independently to ensure that the objective is met. The NUnit Framework caters a range of attributes that are used during unit tests. They are used to define Test -Fixtures, Test methods, Expected Exception and Ignore methods.
Image result for nunit testing framework c#

Important NUnit Annotations
Now let’s take a look at the annotations and how to use them. The annotations are very easy to use: just add the annotation between brackets before the method declaration. With the annotation you can define the test: behavior (specifying Setup or TearDown method), assertions for example performance assertions like MaxTime method, and information like the Category method.

Annotation
Usage
Category
Specifies one or more categories for the test
Culture
Specifies cultures for which a test or fixture should be run
Indicates
Indicates that a test should be skipped unless explicitly run
Ignore
Indicates that a test shouldn't be run for some reason
MaxTime
Specifies the maximum time in milliseconds for a test case to succeed
OneTimeSetUp
Identifies methods to be called once prior to any child tests
OneTimeTearDown
Identifies methods to be called once after all child tests
Platform
Specifies platforms for which a test or fixture should be run
Random
Specifies generation of random values as arguments to a parameterized test
Repeat
Specifies that the decorated method should be executed multiple times
Retry
Causes a test to rerun if it fails, up to a maximum number of times
TearDown
Indicates a method of a TestFixture called immediately after each test method
Test
Marks a method of a TestFixture that represents a test
TestCase
Marks a method with parameters as a test and provides inline arguments
Timeout
Provides a timeout value in milliseconds for test cases
SetUp
Indicates a method of a TestFixture called immediately before each test method
TestFixture
The TestFixture attribute is an indication that a class contains test methods.
Why should I use the NUnit framework?
  • NUnit runs very well with .NET programming languages.
  • It is open source and it is free.
  • It is easy to integrate it with testing projects.
  • NUnit works with many integrated runners including Resharper and TestDriven .NET.
  • NUnit has frequent version updates.
  • NUnit has a graphical user interface.
  • Very easy integration with Visual Studio and other IDEs.

Featured Posts