FI[R]ST: Good Tests Should Be [R]epeatable
Learn how to write good tests by making them repeatable.
Tests don’t appear out of thin air. We are the ones who get to design them, which means they are entirely under our control. We have the power to devise a test’s conditions, which also means that we should also know the test outcome. Part of our job in test design is to provide an assertion that specifies the outcome every time the test is run.
Repeatable test
A repeatable test produces the same results each time it is run. To accomplish repeatable tests, we must isolate them from anything in the external environment that is not under our direct control.
Dealing with rogue elements
Our system will inevitably need to interact with elements not under our control. Any use of the current time, for example, means our test must deal with a rogue element that will make it harder to write repeatable tests.
We can use a
mock
object to isolate the rest of the code under test conditions and keep it independent from the volatility of time.
Get hands-on with 1400+ tech skills courses.