There are three rules applicable to all testing environments. I’ve articulated one of the rules before but now I’ll add two more.
Rule 1 – Dead Simple
Tests must be dead simple to run. It doesn’t matter what you’re testing. It doesn’t matter if you’ve got 100% code coverage. It doesn’t matter if you’ve tested everything from the smallest unit to the largest UI. If the tests aren’t easy to run the people who most need to run them that is the developers and the analysts will figure out ways to game the system and avoid running the tests. The rule is that running tests must be easier than any workaround the developers and analysts can come up with to avoid running the tests.
Rule 2 – Some Tests Are Better Than No Tests
I can’t count the number of times we’ve been discussing testing and someone will say “well, we can’t test X” with the implication being that since we can’t test everything there’s nothing to be gained from any testing. This is analogous to saying the only way we can engineer a rocket is to build the whole thing and then fire it off. Never mind building prototypes or doing any of that silly math to check our assumptions.
Now, granted there can be some cases where certain properties are so essential to a system that if they can’t be tested it really isn’t worth testing at all. But those are extremely rare cases. Even if you can only run tests on 50% of your code, that’s still 50% that’s tested that would not get tested otherwise.
Rule 3 – Don’t Test the Language or the Libraries
I’ve seen unit tests where someone will set some property of a class. Then they will immediately read the property to check that is hasn’t changed. While it’s a valid test to check an invariant on a class, it’s not valid to test it immediately after its set. If you set a value and then read it and it’s different, you’ve got problems more substantial than a unit test will ever solve.
These are just my big three rules for testing. I’d be interested to hear the thoughts of others on this subject.