Beautiful Tests
I can’t dictate what a beautiful test looks like, there are too many of them. But they all have several attributes in common:
- They're Quick
- They are repeatable
- They aren't flakey.
- They don't have side effects
- Works on someone else's machine.
Now rather than assume people are going to write awesome tests, how about we set ourselves up for success by having an environment where only tests that meet the above criteria ever pass?
How to
- We can run the tests from Jenkins / TeamCity to prove that it works on someone else's machine.
- To help make the tests quick before running the tests, we can turn off the network:
For example on Windows (if your admin) this should do the trick:
wmic path win32_networkadapter where NetConnectionID="Local Area Connection" call disable << run tests >>
wmic path win32_networkadapter where NetConnectionID="Local Area Connection" call enable
This would ensure that it wasn’t using non-local databases or web-services.
- I'm not sure if we can set NUnit to do this by default, but we can add the attribute [Maxtime(1000)] to the test to cancel it and mark as a failure if it takes too long. (It would be good to be able to automatically categorise slow running tests! - google do this, so should we! A slow test should be sent to 'test purgatory'.)
Oh, the fun we could have!
With NUnit 3 there’s the ability to run tests in parallel. It’s well overdue functionality. But I want to take a different direction.
Just how much can we squeeze out of one test?
- We can run it multiple times to detect memory leaks.
- We can run multiple times to check caching works.
- We can run the same test concurrently to detect race conditions.
(I feel at the moment we still live in a land where tests are imperative - gurkin needed.)