Fast: The Fourth Quality of Valuable Tests
Learn about the fast valuable test quality and costs and reasons of slow tests.
We'll cover the following
Fast
It’s easy to overlook the importance of pure speed in the long-term maintenance of a test suite or a TDD practice. In the beginning, there isn’t much difference. When we have only a few methods under test, the difference between one second per test and one-tenth of a second per test is almost invisible. The difference between a one-minute suite and a six-second suite is easier to discern.
Slow test suites hurt us in a variety of ways.
Slow tests cost
There are startup costs. In the sample TDD session we walked through in the chapters “Test-Driven Development basics and Test-Driven Rails”, we went back and forth to run the tests a lot. Throughout writing that tutorial, we ran the tests dozens of times. Imagine what happens if it takes even 10 seconds to start a test run, or a minute, which isn’t out of the question for a more extensive Rails app. As JRuby-based applications, that took well over a minute to start.
TDD is about flow now, and the ability to go back and forth between running tests and writing code without breaking focus is crucial to using TDD as a design tool. If we check Twitter while our tests are running, we won’t get the full value of the TDD process.
Reasons for slow tests
Tests become slow for several reasons, but the most important in a Rails context are:
-
The startup time
-
Dependencies within the code that require several objects to be created to invoke the method under test
-
Extensive use of the database or other external services during a test
Not only do large object trees slow down the test at runtime, but setting up large amounts of data makes writing the tests more labor-intensive. If writing the tests becomes burdensome, we aren’t going to do it.
Testing performance
Speeding tests up often means isolating application logic from the Rails stack so that logic can be tested without loading the entire Rails stack or retrieving test data from the database. As with many good testing practices, this isolation results in more robust code that is easier to change moving forward.
Since test speed is so important for successful TDD, we’ll learn ways to write fast tests fastly throughout the course. In particular, creating data in the chapter “Adding Data to Tests” and the discussion of testing environments in the chapter “Running Tests Faster and Running Faster Tests” will be concerned with creating fast tests.
Get hands-on with 1400+ tech skills courses.