This article is cross-posted from my personal blog.
We haven’t been very good at using automated tests in the DotNetNuke project but that will change dramatically this year. We have established a Community Quality team who will be focusing on “Integration Tests” using the WatiN Framework, and in the “Open Core” development area we will be creating Unit Tests for new development, as well as back-filling Unit Tests for the older areas of the core.
Over the last few weeks I have spent quite a bit of time in both areas – building Unit Tests for the code I am developing as part of the current sprint and helping the Community Quality Team develop a solid foundation for the Integration tests.
As part of our increased focus on testing, I will be blogging on “Automated Testing” topics in a new series I have called “Adventures in Testing”.
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.