Patterns enable code quality also for unit tests. Using the same structure in all of your tests saves most of your time in unit testing on the long run, by helping you and others easily understand the content of test cases, as well as maintaining them over time. I elaborate on this fact in more details in my free ebook series “
The 9 Fundamental Rules for Better Code Every Day.” (
subscribe here), as I’ve already discussed in another article.
The most famous pattern is Arrange-Act-Assert. Its simplicity allows it to be applied to any kind of Unit Test, enabling Unit Tests to be more readable, structured, and maintainable.
In its essence, this pattern consists of the following three parts (you guessed right): Arrange, Act, Assert.
- Arrange: First, everything is prepared for the test case to be carried out.
- Act: In this part, the action happens. The code unit to be tested is executed using the previously prepared data, and the results of this execution (the actual result of the execution) are collected.
- Assert: Finally, the actual result obtained in the “Act”-part is compared with the expected results (prepared in the “Arrange”-part). In this final part, it is verified whether the tested code unit works as expected.
In the following, this pattern is demonstrated using some examples written with C# and NUnit.



You can download the code examples as a .cs-file here:
aaa_examples.
What do you think of these examples?
Do you know some good examples that you want to add?
What is your experience with the AAA-pattern?
Let me know in the comments section below.
written by Daniel Lehner
Daniel Lehner