Assertions

Nunit provides the Assert class to validate the expected result through many static methods.

Why do we need Assert:

To validate the expected result and the actual result we can use assert, it will throw an error if expected and actual mismatches.

There are constraint model of Assert,

Comparision
    - Equal
    - Not Equal
    - Greater than
    - Less than
    - Ranges
    

Examples:
            Assert.That(5, Is.EqualTo(5));
            Assert.That(4, Is.Not.EqualTo(5));
            Assert.That(6, Is.GreaterThan(5));
            Assert.That(4, Is.LessThan(5));
            Assert.That(4, Is.InRange(1,5));
 

String
    - String Equal
    - String Equal Ignore case
    - Substring
    - Empty
    - Start with, Ends with
    

Examples:
            Assert.That("Test", Is.EqualTo("Test"));
            Assert.That("Dev", Is.Not.EqualTo("Test"));
            Assert.That("Test", Does.Contain("Test"));
            Assert.That("Dev", Does.Not.Contain("Test"));
            Assert.That("", Is.Empty);
            Assert.That("Test", Is.Not.Empty);
            Assert.That("Test", Does.StartWith("T"));
            Assert.That("Dev", Does.Not.StartWith("T"));
            Assert.That("TesT", Does.EndWith("T"));
            Assert.That("Dev", Does.Not.EndsWith("T"));
            Assert.That("Test", Does.Match("T*T"));
 

Collection
    - Not Null
    - All Greater than
    - Instance of 
    - No Items
    - Exactly n items
    - Unique Items
    - Contains Items
    

Examples:
            int[] intArray = new int[] { 2, 4, 6, 8, 10 };
            int[] emptyArray = new int[] {};
            int[] nullArray = null;
            Assert.That(intArray, Is.All.Not.Null);
            Assert.That(intArray, Is.All.GreaterThan(1));
            Assert.That(intArray, Is.All.LessThan(11));
            Assert.That(emptyArray, Is.Empty);
            Assert.That(intArray, Is.Not.Empty);
            Assert.That(intArray, Has.Exactly(5).Items);
            Assert.That(intArray, Is.Unique);
            Assert.That(intArray, Contains.Item(4));
    
 

Conditional
    - Null 
    - Boolean
    - Empty
    

Examples:
            Assert.That(nullArray, Is.Null);
            Assert.That(true, Is.True);
            Assert.That(emptyArray, Is.Empty);
 

Compound
    - And
    - OR
    - Not      
    

Examples:
            Assert.That(3, Is.GreaterThan(2).And.LessThan(7));
            Assert.That(3, Is.LessThan(4).Or.GreaterThan(7));
            Assert.That(3, Is.Not.EqualTo(7));
            
 

File / Directory
     Examples:
            Assert.That(new FileInfo("path"), Does.Exist);
            
Exceptions
     - Is Exception Throws 
     Examples:
            Assert.That(Exceptionclass, Throws.Exception);

It also supports the classic way of asserting , 
     Assert.AreEqual("Test", "Dev");
     
If the script failed due to assertion then it will not move to the next line.

 

public void sumoftwonumbers()
{
     Assert.That(2+2, Is.EqualTo(5));
     // Rest of the below code will not be executed
     Assert.That(4, Is.InRange(1,5));     
}

 

In such a situation we can use multiple asserts, Nunit will store all the failures and report at last.

 

public void sumoftwonumbers()
{
    Assert.Multiple(() =>
    {
        Assert.That(2+2, Is.EqualTo(5));
        Assert.That(4, Is.InRange(1,5));     
    });
}

 

If there is an unhandled exception occurred in Multiple asserts then the rest of the code will not be executed.

 

Related Tutorials