Assertions in Specflow Framework

To Assert the expected and actual Result in Specflow Framework we need to use Nunit or MsTest Framework or Fluent assertion library.


Assertions:

The Assertions are an approach to confirm that the expected result and the actual result matches or not. In Test Automation we need to compare the Actual results with an Expected result. In order to fail the Testscripts we need assertions, once the test script is asserted then it will not move to the next line and the script will fail. In this Tutorial lets discuss the Nunit assertions.

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));

 

Example:

 

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));     
}

 

More assertions are explained in this tutorial - https://www.qafeast.com/nunit/assertions

 

 

Related Tutorials