I really love using Minitest along with Minitest Focus during the development process with Rails.

All it takes is adding focus before a test (or multiple tests), and running rails test will only run those tests. This makes it very handy when working through a specific issue and continually running only those selected tests.

require "test_helper"

class MyControllerTest < ActionDispatch::IntegrationTest
    test "should render index page" do
        # Test goes here. 
        # Only this test will run

    test "should render show page" do
        # Test goes here.
        # This will get skipped

The way minitest-focus works is that it basically changes the arguments passed to minitest to only include tests defined after the focus statement. Very helpful!

However, one issue that I’ve run across during development of Kwikcal is accidentally leaving a focus statement in a PR. It’s a small line, so easy to miss during code reviews. When this happens, your CI integration might give a false positive as your CI might only execute the focused tests!

Aside from catching this during a code review, a simple way to prevent accidentally leaving focus statements in a pull request is to patch the method to throw an exception while in a CI environment.

To do this, I created a file named prevent_focus_in_ci.rb and placed it in my test/support folder, as these files are automatically loaded in my test helper.

if ENV["CI"] # Or "CIRCLECI"
    class Minitest::Test
        def self.focus
            raise StandardError.new("FOCUS CALLED IN ERROR: #{self.name}")

With this little snippet you’ll get notified through a failing test suite if this method is ever left over in any of your test files. The raised error will let you know the test file it was added to.

Below is example output from Circle CI

1: from /home/circleci/project/test/models/example_test.rb:61:in `<class:ExampleTest>'
/home/circleci/project/test/support/prevent_focus_in_ci.rb:8:in `focus': FOCUS ADDED IN ERROR: [ExampleTest] (StandardError)

Very handy for making sure your entire test suite runs on CI, while still being able to use the very helpful focus method during development.