Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Fernando Shayani
Fernando Shayani

Posted on

Run RSpec tests on changed files only

I always do my best to run tests on my local machine before publishing a PR. However, depending on the size of the test base, this could be unsustainable.

To solve this, I at least run the tests on all*_spec.rb files of my current working branch (or stack of branches) that were changed (or created) since another branch. Usually, it’s main or staging.

Let’s look at the code below:

o feature-2/more-features|   changed file: user_spec.rbo bugfix/fix-users-controller-params|   created file: users_controller_spec.rbo feature-1/branch-1-new-feature|   updated file: product_spec.rb|   updated file: new_payment_service_spec.rbo main
Enter fullscreen modeExit fullscreen mode

This is a purely didactic example

This stack of branches has changed/created 4 spec files. Now, I want to run RSpec on only those four files, not on the entire test base.

To achieve this goal, I created a bash script to use git to list all changed*_spec.rb files and run therspec command against them. Here is the code:

#!/bin/bash# Set default branch namebranch_name="${1:-staging}"# Get changed files with grep for _spec.rb extensionchanged_specs=$(git diff--name-only--diff-filter=d"$branch_name" |grep_spec.rb)# Check if any changed specs foundif[[-z"$changed_specs"]];thenecho"No changed _spec.rb files found between HEAD and$branch_name."exit0fiecho"List of changed spec files:"echo"$changed_specs"# Run rspec with all changed specsbundleexecrspec$changed_specsecho"RSpec tests completed for changed _spec.rb files."
Enter fullscreen modeExit fullscreen mode

Click here to view and download this gist

Save this file and give it a name (I userspec_changed_files.sh).

Now, simply run it on your repo directory.

./rspec_changed_files.sh
Enter fullscreen modeExit fullscreen mode

By default, it will find the changed files related to thestaging branch, but you can adapt the code or override the branch on the command line with another branch name or commit hash:

./rspec_changed_files.sh main
Enter fullscreen modeExit fullscreen mode

Output:

List of changed spec files:spec/models/user_spec.rbspec/controllers/users_controller_spec.rbspec/models/product_spec.rbspec/services/new_payment_service_spec.rb....................Finishedin0.60517 seconds(files took 1.81 seconds to load)10 examples, 0 failuresRSpec tests completedforchanged _spec.rb files.
Enter fullscreen modeExit fullscreen mode

Happy testing!

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
starswan profile image
Stephen Dicks
  • Location
    St Albans, Herts, UK
  • Work
    Software Developer at UK Legal Aid Agency
  • Joined

This is not quite what you want to do. What you need is to run all the tests that deal with the code that you have changed, not just the tests that have changed. There are existing tools for this - pronto can tell you which files have changed, guard will run tests as you change them, and there is an amazing gemgithub.com/toptal/crystalball which does exactly the behaviour I suggested above. But really you should run all the tests - if they are impractical to run locally, then maybe your project should invest some time in fixing that.?

CollapseExpand
 
shayani profile image
Fernando Shayani
I'm a developer since I was a kid, programming in BASIC and Clipper at that time.Now I'm a fullstack developer (Ruby on Rails), DevOps and Linux administrator.

Hi Stephen. Thanks for the tip for Crystalball. I'll give it a try.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a developer since I was a kid, programming in BASIC and Clipper at that time.Now I'm a fullstack developer (Ruby on Rails), DevOps and Linux administrator.
  • Location
    Brazil
  • Work
    Self-employed at Me
  • Joined

More fromFernando Shayani

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp