Speed up your Play! controller tests 100x

In the previous post, I explained how to use Guice with Play! 2.1 for easy dependency injection. At the end, I demonstrated how to test Play! controllers directly, without creating a FakeApplication. This is great for testing actual controller functionality, without the overhead of loading a new Play! application. It also works very well with Play 2.1’s constructible controllers, allowing you to explicitly pass in class instances that you use.

The performance boost is quite significant. I actually didn’t mention how much direct controller tests speed up your unit tests, so I created a simple benchmark using the Play! 2.1 Guice Example application.

I ran the 3 tests from that project (normal Play! tests, explicitly injected controller, and direct controller instance) 10,000 times, and took the median time (measured in nanoseconds, displayed here in milliseconds).

The results? Direct controller tests performed two orders of magnitude faster than standard Play! tests. Normal Play! controller testing took 5.52ms, the explicitly injected controller took 4.51ms, and the direct controller call took 0.09ms.

This result is intuitive (much less overhead), but many large Play! applications fail to test this way. For a well-tested codebase with thousands of tests, this can shave several minutes off each deployment. In conjunction with dependency injection, this removes code and time bottlenecks to having a very well tested codebase.

We wrote this post while working on Kifi — Connecting people with knowledge. Learn more.

Originally published at eng.kifi.com on February 12, 2013.