Thoughts from the Wet Coast

The musings of an ASP.NET Developer from Canada's We(s)t Coast

DNN Development Tips:3 - “AAA” Grade Quality using Arrange, Act, Assert

Last Modified: May 2 2017
Aug 12 2014

Over the last few years at DNN Corp we have placed an increase reliance on automated testing – both Unit Tests and Integration Tests, using NUnit and Moq.

As part of our increased focus on testing, I will be blogging more on “Automated Testing” topics as part of this DNN Development Tips blog series.

In this first article, I will describe the basic pattern of an automated test – the “Arrange, Act, Assert” pattern.

Listing 1 – A Unit Test written using the Arrange, Act, Assert pattern

   1:  public void Reverse_String()
   2:  {
   3:      //Arrange
   4:      string input = "abc";
   5:   
   6:      //Act
   7:      string result = Util.Reverse(input);
   8:   
   9:      //Assert
  10:      Assert.Equals("cba", result);
  11:  }

Listing 1 shows a simple Unit Test designed to test whether the Reverse function correctly reverses the string passed to it.

The first line of code (line 4) of the test sets up (or “arranges”) the pre-conditions for the test – in this case we create a variable (input) to hold the string to be tested “abc”.

Next, in line 7, we execute (or act on) the Result function – the target of out test by calling the function, passing the input string as a parameter and storing the reversed string in the result variable.

Finally, in line 10 the test method checks (or asserts) that the value returned was the expected value.

This clearly demonstrates the simple, but elegant Arrange, Act, Assert pattern, for “AAA” Grade Quality.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Categories

Tags