Explains Basics of NUnit Testing Tools for Beginners

In this article, I have walked through basis of NUnit testing framework tool, NUnit Attributes, Assertions and their syntax with examples.

In the earlier article, I have explained about Unit Testing and Test-Driven Development (TDD). Please check out this before proceeding with NUnit. 

NUnit is a unit testing framework which used for all .NET framework applications. The Latest Version of NUnit is version 3 which is Open Source software and under the MIT license.

To Download NUnit follow the below page,

I have explained to steps which create new application and explained basis of each. The important things which you should know about NUnit is below,

  1. Attributes
  2. Assertions


These are the indications of test classes and test methods. So, each test class and test methods should start the attribute. Below are the commonly used attributes.


This attribute used to indicate the test class. This attribute will be placed above the class name. Classed without this attribute will not be considered as test method while identifying and running test cases. Refer below screenshot.


This attribute used to indicate the test method. This attribute will be placed above each test methods. Methods without this attribute will not be considered as test method while identifying and running test cases. Refer below screenshot.


The method which set with Setup attribute will be executed before each test method run. This will be used to initialize common properties and objects which required to execute test methods. Earlier to NUnit version 2.5, there could create only one setup attribute method. From NUnit 2.5, we can use more than one Setup methods under a Test Fixture. Multiple setup required when use with inheritance.


The method which set with TearDown attribute will be executed after each test method run which is opposite to Setup attribute. This will be used to cleanup the objects which at last. Earlier to NUnit version 2.5, there could create only one Teardown attribute method. From NUnit 2.5, we can use more than one Teardown methods under a Test Fixture. Multiple Teardown required when use with inheritance.


The tests and test fixtures with Ignore attribute will not run which running all the tests in test explorer. We can use this to temporarily stop some tests to run for testing purpose.


Categories are used to group the tests. This will be used to run the tests of selected categories using NUnit GUI. This is not possible with Test Explorer.

[TestCase(values1, vaule2..)]

This allows to assign inline values to the test methods arguments and run the method. We can use more Testcases attributes with inline values set. So, test method will run to all the inputs one by one.

[Test, Timeout(milliseconds)]

We can set the Timeout value in milliseconds to run the test method. If the test method does not complete the run in given timeline, that test method will be failed.

Obsolete Attributes in NUnit version 3

  1. [TestFixtureSetUp] – Like Setup Attribute, which is used it initialize values, but this method will be executed only once before start any of the tests under the test fixtures.
  2. [TestFixtureTearDown] – Like TearDown Attribute, which is used it clear values, but this method will be executed only after all the tests run completed under the test fixtures.
  3. [ExpectedException(ExceptionType)] – This method with this attribute, expects the result as mentioned exception type. If expected exception type not returned, test will be failed. We have alternate option to achieve this in NUnit 3. Check out the below code,
class StudentTest
     Calculator objCalc;
     public void Initialize()
         objCalc = new Calculator();
     public void TestmethodWithException()
          Assert.Throws<DivideByZeroException>(() => objCalc.Divide(2,0));
class Calculator
     public int Result { get; set; }
     public void Divide(int a, int b)
          Result = a / b;


Assertions are the built-in static method which used to validate the tests with expected results. A test can have multiple assertion, test run will be failed if any one of the asserts doesn’t result expected value which we defined.

Conditional Constraints

  1. Is.Null   

       Assert.That(null, Is.Null);

  1. IsNotNull

Assert.That(10, Is.Not.Null);

  1. IsTrue

Assert.That(5+5==10, Is.True);

  1. IsFalse

Assert.That(5+5==11, Is.False);

  1. IsEmpty

Assert.That(string.Empty, Is.Empty);

  1. IsNotEmpty

Assert.That(“Text”, Is.Not.Empty);

String Constraints

  1. Contains

Assert.That(“Text”, Does.Contain(“ex”));
Assert.That(“Text”, Does.Not.Contain(“abc”));
Assert.That(“Text”, Does.Contain(“X”).IgnoreCase);

  1. StartWith

Assert.That(“Text”, Does.StartWith(“Te”));
Assert.That(“Text”, Does.Not.StartWith(“abc”));
Assert.That(“Text”, Does.StartWith(“TE”).IgnoreCase);

  1. EndWith

Assert.That(“Text”, Does.EndWith(“xT”));
Assert.That(“Text”, Does.Not.EndWith(“abc”));
Assert.That(“Text”, Does.EndWith(“XT”).IgnoreCase);

  1. EqualTo

Assert.That(“Text”, Is.EqualTo(“Text”));
Assert.That(“Text”, Is.Not.EqualTo(“Txt”));
Assert.That(“Text”, Is.EqualTo(“TEXT”).IgnoreCase);

  1. Match

Assert.That(“Text”, Does.Match(“T*t”));
Assert.That(“Text”, Does.Not.Match(“T*s”));

Comparison Constraints

  1. Equal To

Assert.That(5+5, Is.EqualTo(10));
Assert.That(5+3, Is.Not.EqualTo(10));

  1. Greater Than

Assert.That(10, Is.GreaterThan(5));
Assert.That(10, Is.GreaterThanOrEqualTo(10));

  1. Less Than

Assert.That(10, Is.LessThan(15));
Assert.That(10, Is.LessThanOrEqualTo(15));

  1. Range

Assert.That(10, Is.InRange(5,15));

Collection Constraints

Consider the Collection Array variable

int[] intCollections = new int[] { 1, 2, 3, 4 };
string[] stringCollections = new string[] { “car”, “bus”, “bike”, “auto” };

  1. Null Check

Assert.That(intCollections, Is.All.Not.Null);
Assert.That(intCollections, Has.None.Null);

  1. Greater Than

Assert.That(intCollections, Is.All.GreaterThan(0));
Assert.That(intCollections, Is.All.GreaterThanOrEqualTo(0));

  1. Less Than

Assert.That(intCollections, Is.All.LessThan(5));
Assert.That(intCollections, Is.All.LessThanOrEqualTo(5));

  1. Instance of

Assert.That(intCollections, Is.All.InstanceOf(typeof(int)));
Assert.That(stringCollections, Is.All.InstanceOf(typeof(string)));

  1. Unique

Assert.That(intCollections, Is.Unique);
Assert.That(stringCollections, Is.Not.Unique);

  1. Contains

Assert.That(intCollections, Contains.Item(3));
Assert.That(stringCollections, Has.All.Contains(“b”));

  1. StartWith

Assert.That(stringCollections, Has.Some.StartWith(“b”));

  1. Empty

Assert.That(intCollections, Is.Empty);
Assert.That(intCollections, Is.Not.Empty);

Type Constraints

  1. Type Of

Assert.That(“Text”, Is.TypeOf(typeof(string)));
Assert.That(“Text”, Is.Not.TypeOf(typeof(int)));

  1. Instance Of

Assert.That(10, Is.InstanceOf(typeof(int)));
Assert.That(“Text”, Is.Not.InstanceOf(typeof(int)));

Exception Constraints

  1. Throws

Assert.That(obj.Method(), Throws.Exception);
Assert.That(obj.Method(), Throws.TypeOf<DivideByZeroException>); Assert.Throws<DivideByZeroException>(() => obj.Method());

I will explain to create simple C# application which helps to use these basic syntax by following TDD workflow in my next article.

Leave a Reply

Your email address will not be published. Required fields are marked *