Testing Against Contract
We like to write test cases for a module to ensure that the modules honors its contract. This will tell us
We like to write test cases for a module to ensure that the modules honors its contract. This will tell us
- whether the code meets the contract.
- whether the contract means what we think it means.
We test the module to check whether it delivers the functionality it promises, over a wide range of test cases and boundary conditions.
How do we define contract? For example, let’s take the factorial of a number problem. It’s contract can be defined as follows:
- Pass in a negative argument and ensure that it is rejected.
- Pass in an argument zero and ensure that it is accepted (boundary condition).
- Pass in an argument between zero and upper limit and ensure that the correct factorial is returned.
We have to test all these scenarios to ensure that our factorial module is working.
Why do we go to all this trouble? We are investing time and effort to write test cases. All we want is to avoid ‘time bombs’ in our code that sits unnoticed and blows up at an unexpected moment later in the project.
Suppose our module uses a linked list and a sort algorithm, we have to test the linked list, sort and the module completely. If the linked list and sort pass and the module test cases fails, we’ll know that the problem is with the module. This helps to reduce the debugging time also.
Suppose our module uses a linked list and a sort algorithm, we have to test the linked list, sort and the module completely. If the linked list and sort pass and the module test cases fails, we’ll know that the problem is with the module. This helps to reduce the debugging time also.
While designing a module, always design both its contract and the code to test that contract.
- summary of Testing Against Contract, from The Pragmatic Programmer: from Journeyman to Master
- summary of Testing Against Contract, from The Pragmatic Programmer: from Journeyman to Master
No comments:
Post a Comment