Skip to content

Conversation

@Magikdidi24
Copy link

@Magikdidi24 Magikdidi24 commented Oct 30, 2025

Fix test class detection to avoid false positives

Summary

This PR fixes the test class detection logic in MinitestExplorationHelpers to eliminate false positives and false negatives. The detection now relies solely on the parent class rather than class naming conventions.

Problem

The previous implementation had two major issues:

1. False Positives

Classes with "Test" in their name but not inheriting from test frameworks were incorrectly identified as test classes:

class FooTest < ApplicationRecord  # Incorrectly identified as test class
  def perform; end
end

class ModelFooTest < ActiveJob::Base  # Incorrectly identified as test class
  def perform; end
end

This caused RuboCop to run Minitest cops on non-test code.

2. False Negatives

Classes following Minitest's TestFoo naming convention were not recognized:

class TestFoo < Minitest::Test  # Not recognized as test class
  def test_something
    assert true
  end
end

This caused RuboCop to skip checking legitimate test classes.

Usage Example

Project with custom test base class:

# test/support/parent_test.rb
class ParentTest < Minitest::Test
  def setup
    # Common setup
  end
end

# test/models/user_test.rb
class UserTest < ParentTest
  def test_something
    assert true
  end
end

Configuration needed:

# .rubocop.yml
Minitest:
  AdditionalTestBaseClasses:
    - ParentTest

Without this configuration, UserTest < ParentTest will not be recognized as a test class.

Migration guide for users:

If you have custom test base classes like:

class ParentTest < Minitest::Test; end
class MyCustomBase < Minitest::Test; end

And your test classes inherit from them:

class UserTest < ParentTest; end
class FooTest < MyCustomBase; end

Add this to your .rubocop.yml:

Minitest:
  AdditionalTestBaseClasses:
    - ParentTest
    - MyCustomBase

Related Issues

Fixes issue mentioned in comments about:

  • Classes named FooTest < ApplicationRecord being incorrectly identified as test classes
  • TestFoo naming convention not being recognized
  • Need for configurable test base classes

Documentation

Users should be informed about the AdditionalTestBaseClasses configuration option for custom test base classes.
Related to #320


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.

@Magikdidi24
Copy link
Author

Hey, @koic , @flavorjones, Any issue with this PR?
The detection based on parent classes is more accurate than using naming rules and avoids both false positives and false negatives. The AdditionalTestBaseClasses option covers custom setups. Let me know if changes are needed.

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.

1 participant