Skip to content

Optimize MinitestExplorationHelpers#test_cases helper - #364

Merged
koic merged 1 commit into
rubocop:masterfrom
moberegger:moberegger/optimize-test_cases-helper
Aug 24, 2026
Merged

Optimize MinitestExplorationHelpers#test_cases helper#364
koic merged 1 commit into
rubocop:masterfrom
moberegger:moberegger/optimize-test_cases-helper

Conversation

@moberegger

@moberegger moberegger commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

A follow up to #363

Optimizes the MinitestExplorationHelpers#test_cases helper. The intent of this helper was to find test and it class definitions, but this would actually descend into method bodies, which incurs a rather substantial traversal cost. The optimization basically terminates the traversal earlier.

This does technically introduce a change in behavior. The old implementation could result in false positives/negatives where the proposed optimized version does not. For example, with something like

class FooTest < Minitest::Test
  def build_matrix(cases)
    cases.each do |name|
      it(name) { skip }
    end
  end
end

originally the cop would see it and think it's a valid test definition even though it is not. it is a class level DSL, and in this case an it instance method would be called. I am having trouble thinking of a good example here; I can't imagine this happening too often given that this is not valid syntax (at least as far as the minitest DSL is concerned), but I suppose there is a corner case here where the developer implemented their own it instance method helper or whatever that allowed this to run. I mention this mostly for completeness.

Benchmarks below. This was run against 5,000 cop executions per rule. Note that the Minitest/MultipleAssertions benchmark ran with the changes in #363 included.

Cop Before After Speedup Offenses
Minitest/MultipleAssertions 0.326s 0.256s 1.27x 3750
Minitest/NoAssertions 0.255s 0.190s 1.34x 250
Minitest/NoTestCases 0.159s 0.097s 1.64x 0
Minitest/DuplicateTestRun 0.253s 0.121s 2.08x 250
Minitest/NonPublicTestMethod 0.159s 0.096s 1.66x 250

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@moberegger
moberegger marked this pull request as ready for review August 17, 2026 17:53
@koic
koic merged commit b22556c into rubocop:master Aug 24, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants