Techniques for better software testing

31 points by amw-zero


mediremi

Another technique is to enable sanitizers while running tests (e.g. TSan, ASan).

Go in particular makes this pretty easy: just do go test -race ./... to enable the data race detector while running unit tests.

hmpc

A well-known but underused instance of validation from within that I have found particularly useful is to have comprehensive internal consistency checks for custom data structures that only run on debugging builds; for example, visiting an entire tree to make sure the ordering invariants hold or the colouring is consistent. This works quite well with any form of randomised testing, as it forcefully exposes internal faults early that might otherwise not be externally visible.

(These kinds of assertions should crash execution as forcefully as possible instead of e.g. throwing an exception that can be innocently but disastrously caught by some overeager handler up-stack. Ideally the testing framework will run the tests in separate processes to catch these situations, which is also helpful to parallelise the test suite.)

And on the subject of debugging builds, make sure you also run your tests with production builds. Sounds obvious and it is, but you'd be surprised how easy it is to forget about it until you're caught out by some UB that never shows up without optimisations...