Ruby on Rails: Running specific tests aka “I just want to test my current module”

There comes a time when your project has a reasonable amount of tests, be it unit tests, integration tests and what not. When working on a specific component, let’s say a payment module for an online shop you could probably do without running the whole test suite every single time you’re creating, changing or deleting a test for the payment module.

Now, if you just want to run a single test you can do so easily from the command-line using:

You could probably extend that one liner to include a list of files and create a shell script that does it for you, but why do that when we can create a rake test task that does it for us? If we open up our Rakefile and add a new TestTask under the test namespace like this:

This will allow us to run

from the command-line and it will run through the tests we’ve defined in the file list. You could probably be smart about the list using wildcards (or expressions?), but for now we only have the two files. This requires that the tasks be maintained, but in return you don’t have to run all the other tests each time you want to see if something failed or passed in the payment module.

I realize there might be other ways and perhaps some cool gems that can help us out even further so if you have tips, suggestions or the absolute truth then please feel free to comment and I’ll do my best to update the post!