Last Updated: November 15, 2017
· guiman

Running modified specs with git

So lately have been dealing with lots of rspec files, but since the whole suit of tests would take more than an hour to run, I have to cherry pick the ones I need.

So I came up with this shortcut in bash

bundle exec rspec $(git status | grep spec | grep "modified:" | cut -b 15-)

Basically fetches the spec files that have been modified, using git status, and run them.

3 Responses
Add your response

I think it can be nice, I'm thinking about to implement some quick tests, before running the actual full list of tests.

For me, it would be :

  • If there is a spec or a feature modified, run it.
  • If there is a controller modified, run the associated spec and feature.
  • If there is a model modified, run the associated spec and feature.
  • If there is a view modified, run the associated features.

It doesn't cover all the cases, but the basics ones.

One more thing, I think you could drastically improve performance by 1st building a list of files, and then passing it to rspec. (If you have a lot of files, you'll load rspec and rails just once.)

over 1 year ago ·

Thank you very much for the comment, I'll try and work out some of the updates you have mentioned :)

over 1 year ago ·

This is awesome! The length of the whitespace around "modified:" varies by version of git. Here's a more precise and reliable cut:

git status | grep spec | grep "modified:" | cut -d: -f2-
over 1 year ago ·