Business Class Blog

The pragmatic approach to testing Rails applications

Table of contents

Testing is important, but what's the right amount of test coverage to move fast, validate startups, and keep everyone happy?

Can we test too much?

When I was in high school and programming in PHP testing wasn't clicking for me. That changed very quickly by switching to Ruby and especially the Rails framework. Today one of my published books is about testing Rails applications.

I hope you'll agree with me that testing is important. But what's the right amount of testing? Sometimes I see people test too little and sometimes a bit too much. And is there even something as too many tests?

I think there is. There is a cost to every test and it's no wonder that DHH & 37signals will now rebuild their browser test suite as smaller number of smoke tests. Ultimately tests need to be useful and net positive for a given project.

What needs a test

To start, I would like to write what I think it's the bare minimum testing in a project. For me that's anything that has to do with authentication and authorization. Since Business Class already comes with a good test coverage and generates everything with Active Policy and policy tests, this is easy to follow without much work.

I would also add basic controller tests to the minimal test starter given there are easy to write and fast to run. The basic controller tests are again practically free because they are either generated by Rails or Business Class generator. And even in their most basic form can catch lots of different bugs out of the box.

Model tests

The most natural next step is to properly cover your models or logic in similar types of files (services, strategies, etc.). The level of test coverage should be highly dependent on your application. Is it an MVP or an established startup with enterprise customers?

Spending 20% of effort for testing 80% of the application surface is a good general rule. But I won't spend too much time on extra tests for application with no users. Not anymore, at least :).

Browser tests

And that brings me to system tests. Are they dead now? I don't think so, but their price is high and we need to acknowledge that. Not just in execution time, but even more so for their brittleness.

Business Class still generates basic system tests for CRUD, except for the admin area. The reason being that it's easy to extend them for what needs to be tested and fast to remove redundant ones we don't need.

Future releases

I want to keep a solid test coverage for Business Class but future releases will see most of system tests rewritten as controller ones. First and foremost, I want to see a super fast test suite.

Business Class will still offer basic smoke tests and make it easy to keep, extend or delete system tests from the generator. But we'll go even more pragmatic in the footsteps of DHH.


© Business Class Blog