Read more

open-next-failure: An alias to speed up test debugging

Michael Leimstädtner
December 21, 2023Software engineer at makandra GmbH

Getting an entire test suite green can be a tedious task which involves frequent switches between the CLI that is running tests back to the IDE where its cause can be fixed.

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

The following bash aliases helped me speed up that process:

alias show-next-failure="bundle exec rspec --next-failure"
alias open-next-failure="show-next-failure || show-next-failure --format json  | jq -r '.examples[0]' | jq '\"--line \" + (.line_number|tostring) + \" \" + .file_path' | xargs echo | xargs rubymine"

There is a lot going on above but the gist is: You can use open-next-failure to open the next failing spec with your IDE. Fix it and repeat the command until everything is green!

How does it work

# Define an alias to run the next failure
alias show-next-failure="bundle exec rspec --next-failure"

# IFF there is a failure, print it to the console, then 
# run it again - this time using the machine readable JSON formatter
show-next-failure || show-next-failure --format json 
# Extract the (first and only) failing example
| jq -r '.examples[0]' 
# Concatenate the failing file path and line number as an argument for the rubymine binary
| jq '\"--line \" + (.line_number|tostring) + \" \" + .file_path'
| xargs echo 
# Open the failing spec via `rubymine --line 123 ./path/to/test_spec.rb`
| xargs rubymine

Caveats

  • You need to configure a example_status_persistence_file_path as it allows RSpec to keep track of failing tests. See this card for set-up instructions.
  • If you don't use RSpec for all of your tests, the approach above won't cover all cases
  • Capybara might break the JSON formatter with Capybara starting Puma... statements. This can be turned of by setting Capybara.server = :puma, { Silent: true }
  • If you don't use Rubymine, you need to adjust the argument matching and opener accordingly
Posted by Michael Leimstädtner to makandra dev (2023-12-21 15:10)