Listen to this post
TDD tests need to be run frequently. As an agile process, TDD needs to be responsive and flexible. Part of this is the ability to keep very short the feedback loop between tests and production.
To run tests frequently without excessive cost requires that they must be executed quickly. Here are some things that help keep tests fast.
- If you establish a value in a test instrument, and the test will not change this value, make it a constant. Compilers can take advantage of this and more fully optimize the test execution.
- Don’t allocate memory you don’t need. Example: Sometimes we need to build an instance just to confirm the build, but we won’t need the instance for anything later. Don’t assign a reference to it. This eases the burden on memory management.
- Stay away from slow dependencies. Things such as the database, network, and file system, will dramatically slow down the tests. Mock out these connections, and defer their testing to integration.
- Avoid loops, conditionals and other complex code structures in your tests. They should execute from top to bottom. This makes them faster, and also easier to read as specifications.
So long as they don’t reduce quality, decisions along the way that emphasize speed will pay off over the long term.