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

Updated . Posted . Visible to the public. Repeats.

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.

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
Michael Leimstädtner
Last edit
Maximilian Berger
License
Source code in this card is licensed under the MIT License.
Posted by Michael Leimstädtner to makandra dev (2023-12-21 14:10)