Running Tests Should be Fast!

Let me name you some of the worst things in world: Final season of Two and a Half Men, stubbing your toe at night, text on meme which is too small to read and test suites where you have to wait for 5+ seconds before any of your tests run.


On OSEM, I wanted to improve the test coverage, which is Coverage Status. Red is not really my favorite color (unless it is in a git diff).  If you don’t see red anymore, you know what happened 😉

It was really slow to run tests. I introduced spring, and it did save me file load time but it wasn’t good enough. The aha moment was when I got:

The following factories are invalid: (FactoryGirl::InvalidFactoryError)event_commercial – Validation failed: Network is unreachable – connect(2) for “” port 80 (ActiveRecord::RecordInvalid)

I have gotten that error for a total of one time. Which kind of makes me worry about how less often I stay offline.

You would think we must have introduced webmock and moved on. But wait! There is more. I also found out that we can just decouple running FactoryGirl lint from the test suite. Until now, the lint was running before test suite:

As the Readme of FactoryGirl suggested, I moved lint to rake task and added it to travis. In my head, it made lot of sense. For me, running FactoryGirl lint before every suite is like running rubocop before every request with localhost:3000. As issue #772 on FactoryGirl, very rightly points out.. what if I am doing TDD?  No! TDD is not dead. I am not saying, I use TDD, still running complete lint just when I am running single test is an overhead I can’t bear. May be it just amounts for a couple hundred milliseconds, but those are the milliseconds I shouldn’t have to lose.

Some of the valid arguments against my opinion was that we will potentially be writing tests on broken factories and now we will have to run lint manually. It would indeed suck to write 200 lines  of tests only to find out that your factories are not correct. I think it can be solved to an extent by integrating lint with pre-commit hook.

In the end we were able to find a common ground by moving FactoryGirl lint behind an ENV variable:

What would be value of your ENV[OSEM_FACTORY_LINT]?

Running Tests Should be Fast!