No side effects in test code

Tuesday, 31 August 2004

When a TestCase runs, it should either succeed or fail: there should be no side effects. For example, Testcases should not write to the console or to a log file, or save anything to a database. There are two reasons for this.

First, these tests are supposed to be automated. It defeats the purpose if you have to look at the console or a log file to see if the test succeeded. An ideal test suite is such that you push a button, wait a while, and get a message that says either “all tests succeeded” or “texts x, y and z failed”. If it’s any more complicated that this — if you have to look in a file or read through console output — then you’re less likely to bother running the tests, and more likely to overlook failed tests.

Second, test code may run hundreds of times. If tests leave residue behind in a database or file system, it will build up and somebody will have to clean it up later. Again, this complicates the process of running the tests and makes it less likely that they weill be used.


Leave a comment