My Thoughts: Slow Tests are Bad Tests

My Thoughts: Slow Tests are Bad Tests

My tests are slow!

They were slow anyway... Part of the Ruthless Refactoring I've been doing while working on the HackerNews App has been to improve the performance of the unit tests.

I easily shaved off a second from the tests during the "Clean Architecture" update. It ended up exposing (or more removing) a background thread action I had to wait on. It was a mocked network call, so took a small amount of time. I didn't have a clean way to "hook" into the background code to know exactly when it finished. This resulting in a Thread.sleep(500);
I cringed; but needed the delay.

During the clean architecture refactor; I was able to refactor out the terrible sleeping.

Pretty simple rule from this

Don't sleep; it make tests slow.

I had some longer running tests that I got to run a bit faster by refactoring the commonality and object creation into the set up using the @Mock annotation.
I don't know if it was exactly that which sped up the tests; but that's all I recall doing... So.. Yes, refactor the commonality; maybe it gets better optimized? (I'm making that up).

Early on, my 140ish tests took about 7 seconds to run. Co-worker (Hi Steve!) mocked me mercilessly. I mentioned the tests, and he dares to ask me, "How quickly do they run?"
That's just so... He... I... ... Speechless. Such an affront... ... which I then felt really bad about having them at 7 seconds. So I got them down to 5 in a couple minutes. That's where some early refactoring killed a couple seconds.

Before the Clean Architecture refactor my tests were up around 3 seconds. I was planning on paying a lot more attention to the test times when I got around to writing this exact post. Unfortunately ... or fortunately? The ruthless refactoring and continious improvement had the side effect of improving test run times.

My tests are down to ~1.6 seconds; for 139 tests.

I don't know what a "good" number is. Fast enough. If you're not willing to run the tests after every small change; then they take too long.
There are ways to create 'suites' to run subsets; or with Android Studio just run sub-sets of tests. This can help run tests faster for more frequent running; which is the big things we want for the tests.

Show Comments