Table of contents
Test case is the start
The content here is under the Attribution 4.0 International (CC BY 4.0) license
In the practice of test driven evelopement (TDD), the test case is the starting point of the development process. It is not just a verification step at the end, but rather a fundamental part of the design and implementation of the software. The test case defines the expected behavior of the system, guiding the development process and ensuring that the code meets the specified requirements.
There is a fine line between a test case and an assertion. A test case is a broader concept that encompasses the entire testing process, including setup, execution, and verification. An assertion, on the other hand, is a specific statement within a test case that checks whether a certain condition holds true.
The test case binds into code the requirement that the code must fulfill. It serves as a contract between the developer and the code, ensuring that the code behaves as expected. The test case is not just a collection of assertions, but rather a structured and organized way to verify the functionality of the code.
How to engineer a test case
Test cases are engineered to be clear, concise, and focused on specific behaviors or outcomes. They should be easy to read and understand, allowing developers to quickly grasp the purpose of the test. Good test cases are also maintainable, meaning they can be easily updated as the code evolves.
When writing a test case, it is important to consider the following:
- Purpose: What is the specific behavior or outcome being tested?
- Setup: What is the initial state of the system before the test is executed?
- Execution: How is the test executed, and what actions are performed?
- Verification: What assertions are made to verify the expected behavior?
- Cleanup: How is the system restored to its original state after the test is executed?
- Documentation: Is the test case well-documented, providing context and explanations for future reference?
In the literature, we find the pattern AAA (Arrange, Act, Assert) as a common structure for test cases. This pattern helps to organize the test case into three distinct phases (Meszaros, 2007):
- Arrange: Set up the necessary conditions and inputs for the test.
- Act: Execute the code or action being tested.
- Assert: Verify that the expected outcome matches the actual result. This structure promotes clarity and readability, making it easier for developers to understand the purpose and flow of the test case.
References
- Meszaros, G. (2007). xUnit test patterns: Refactoring test code. Pearson Education.