Getting started with nunit

Download nunit eBook

Remarks

This section provides an overview of what nunit is, and why a developer might want to use it.

It should also mention any large subjects within nunit, and link out to the related topics. Since the Documentation for nunit is new, you may need to create initial versions of those related topics.

Versions

VersionRelease Date
2.22004-08-08
2.2.12004-10-26
2.2.22004-12-07
2.2.32005-02-14
2.2.42005-12-14
2.2.52005-12-22
2.2.62006-01-21
2.2.72006-02-18
2.2.82006-04-21
2.2.92006-11-26
2.2.102007-03-15
2.4 RC12007-02-25
2.4 (Final Release)2007-03-16
2.4.12007-05-03
2.4.22007-08-02
2.4.42007-10-20
2.4.52007-11-23
2.4.62007-12-31
2.4.72008-03-30
2.4.82008-07-21
2.52009-05-02
2.5.12009-07-08
2.5.22009-08-10
2.5.32009-12-11
2.5.42010-04-08
2.5.52010-04-22
2.5.62010-07-24
2.5.72010-08-01
2.5.82010-10-14
2.5.92010-12-14
2.5.102011-04-02
2.62012-02-20
2.6.12012-08-04
2.6.22012-10-22
2.6.32013-10-10
2.6.42014-12-16
3.0 (Alpha 1)2014-09-22
3.0 (Beta 1)2015-03-25
3.0 RC12015-11-01
3.0.0 Final Release2015-11-15
3.0.12015-12-01
3.22016-03-05
3.2.12016-04-19
3.42016-06-25

Hello World

[TestFixture]
public class UnitTest1
{
    class Message
    {
        public string Text { get; } = "Hello World";
    }

    [Test]
    public void HelloWorldTest()
    {
        // Act
        var message = new Message();

        // Assert
        Assert.That(message.Text, Is.EqualTo("Hello World"));
    }
}
 

Hello World test

Installation Using NuGet

Install-Package NUnit
 

This package includes all assemblies needed to create unit tests.

Tests can be executed using one of the following methods:

  • Visual Studio Unit Test Window
  • Console runner
  • Third party runner that supports NUnit 3

Visual Studio Unit Test Window

To execute tests using the Visual Studio Unit Test Window, install the NUnit 3 Test Adapter. https://visualstudiogallery.msdn.microsoft.com/0da0f6bd-9bb6-4ae3-87a8-537788622f2d

Console Runner

Install the NUnit Console Runner via NuGet

Install-Package NUnit.Console
 

The executable nunit3-console.exe is located in packages\NUnit.3.X.X\tools

TestCaseAttribute

[TestCase(0, 0, 0)]
[TestCase(34, 25, 59)]
[TestCase(-1250, 10000, 8750)]
public void AddNumbersTest(int a, int b, int expected)
{
    // Act
    int result = a + b;
            
    // Assert
    Assert.That(result, Is.EqualTo(expected));
}
 

AddNumbersTest passed

Why you can’t use Assert.Equals

Ever wondered why you cannot use Assert.Equals() for both Nunit and MSTest. If you have not then maybe as a start you need to be aware that you cannot use this method. Instead you would use Assert.AreEqual() to compare two objects for equality.

The reason here is very simple. Like any class the Assert class is inheriting from System.Object that has a public virtual Equals method meant to check if a given object is equal to the current object. Therefor calling that equals method would be a mistake as in a unit test you would instead to compare two objects that have nothing to do with the Assert class. As a result Nunit and MSTest both chose to provide a method Assert.AreEqual for that purpose.

Furthermore to ensure that you do not use the Equals method by mistake they have decided to throw Exceptions to warn you if you do use this by mistake.

Nunit Implementation:

        [EditorBrowsable(EditorBrowsableState.Never)]
    public static new bool Equals(object a, object b)
    {
        // TODO: This should probably be InvalidOperationException
        throw new AssertionException("Assert.Equals should not be used for Assertions");
    }
 

Stats

46 Contributors: 7
Tuesday, September 27, 2016
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook