Blog

Using Arrange-Act-Assert Pattern And Assertions In NUnit

By Anoop Kumar Sharma    3477  05-August-2022

In this article, we will learn about AAA Pattern in the Unit Test case.



In this article, we will learn about AAA Pattern in the Unit Test case. If you are new to this series, I will recommend you to go through the below articles of this series.

Getting Started With Unit Testing With NUnit In .Net Core

Arrange-Act-Assert (3A Pattern) is simple and provides a uniform structure for all the tests in the suite. 

Arrange: Arrange is the section where you set up the objects which need to be tested. In this stage, you bring the system under the test by either directly creating or mocking the object.

Act: In the Act section, we act upon the system under the test. The act should cover the main things (method or dependencies) which have to be tested.

Assert: Assert section is used to verify the result. It verifies that the action of the method under the test behaves as expected. In the below image, you can clearly see the Arrange-Act-Assert pattern which we have written in the previous article.

In the above example, I used Assert.AreEqual() helper method to determine whether the method under the test is performing correctly or not. Assertions are central to the unit testing whichever Unit testing framework you choose. Asserts are the fundamental building block for the unit test cases. NUnit provides a rich set of assertions as the Static Method of the Assert class. NUnit supports both Classic Assert as well as a constraint-style assertion. Let’s understand some of the popular methods (most of the methods have multiple overload versions, I am only sharing only one of each for the understanding purpose).

Classic Asserts

1. AreEqual: Assert.AreEqual() is one of the most often used asserts. This method has several overload versions which can be used as per the requirement.

public static void AreEqual(object? expected, object? actual)

2. Less/Greater: Assert.Less() and Assert.Greater are two helper methods that are used to compare the expected and actual value.

public static void Greater(int arg1, int arg2);
public static void Less(int arg1, int arg2);

3. GreaterOrEqual/LessOrEqual: Used to assert that arg1>=arg2 for checking the GreaterOrEqual condition and arg1<=arg2 to check the LessOrEqual condition.

public static void GreaterOrEqual(int arg1, int arg2);
public static void LessOrEqual(int arg1, int arg2);

4. IsNull/isNotNull: Assert.isNull() is used to check whether the given object is null and Assert.IsNotNull() is used to check that the given object is not null.

public static void IsNull(object? anObject);
public static void IsNotNull(object? anObject);

5. IsTrue/isFalse: Assert.IsTrue() is used to check the Boolean condition is returning true. A test case will fail in case the Boolean condition returns false whereas Assert.IsFalse() is used to check the Boolean condition is returning false.

public static void IsTrue(bool condition);
public static void False(bool condition);

6. Fail: Assert.Fail() fails the test immediately. It provided the ability to generate a failure test and used to validate the own project-specific assertions.

public static void Fail();

 

Constraint-Based Assertions

NUnit 2.4 introduced constraint-based assertions that are a little less procedural and supports more object-oriented underlying implementation. Let’s see some of the popular constraint-based assertions.

1. Is.EqualTo: Is.EqualTo is similar to Assert.AreEqual() classic assertion method. It’s also a static method that returns an EqualConstraint object.

2. Is.Not.EqualTo: Is.Not.Equal.To is equivalent to Assert.AreNotEqual() classic assertion method. We can also apply Not to any Is or Has syntax helper.

3. Is.AtMost: Is.AtMost constraint-based assertion is equivalent to Assert.LessOrEqual() classic assertion method. Is.AtMost returns LessThanOrEqualConstraint object.

4. Is.Null: Is.Null is used to assert that expected is null and if expected is not null then fails the assertion.

5. Is.Empty: Is.Empty asserts that the collection or string is empty. If not empty then the test case will be failed.

In the Assert class, there are several methods and each method has multiple overloads for which you also check the documentation on the NUnit’s official website. Below are the useful links which you can use for quick reference.

https://docs.nunit.org/articles/nunit/writing-tests/assertions/assertion-models/classic.html

https://docs.nunit.org/articles/nunit/writing-tests/constraints/Constraints.html

or you can directly look and explore the available methods in Assert Class through Visual Studio.