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()
4: string input = "abc";
7: string result = Util.Reverse(input);
10: Assert.Equals("cba", result);
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.