Class: RuboCop::Cop::Minitest::DuplicateTestRun

Inherits:
Base
  • Object
show all
Includes:
RuboCop::Cop::MinitestExplorationHelpers
Defined in:
lib/rubocop/cop/minitest/duplicate_test_run.rb

Overview

If a Minitest class inherits from another class, it will also inherit its methods causing Minitest to run the parent’s tests methods twice.

This cop detects when there are two tests classes, one inherits from the other, and both have tests methods. This cop will add an offense to the Child class in such a case.

Examples:

# bad
class ParentTest < Minitest::Test
  def test_parent # it will run this test twice.
  end
end

class ChildTest < ParentTest
  def test_child
  end
end

# good
class ParentTest < Minitest::Test
  def test_parent
  end
end

class ChildTest < Minitest::Test
  def test_child
  end
end

# good
class ParentTest < Minitest::Test
end

class ChildTest
  def test_child
  end

  def test_parent
  end
end

Constant Summary collapse

MSG =
"Subclasses with test methods causes the parent' tests to run them twice."

Constants included from RuboCop::Cop::MinitestExplorationHelpers

RuboCop::Cop::MinitestExplorationHelpers::ASSERTION_PREFIXES, RuboCop::Cop::MinitestExplorationHelpers::LIFECYCLE_HOOK_METHODS, RuboCop::Cop::MinitestExplorationHelpers::LIFECYCLE_HOOK_METHODS_IN_ORDER

Instance Method Summary collapse

Instance Method Details

#on_class(class_node) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/minitest/duplicate_test_run.rb', line 53

def on_class(class_node)
  return unless test_class?(class_node)
  return unless test_methods?(class_node)
  return unless parent_class_has_test_methods?(class_node)

  message = format(MSG)
  add_offense(class_node, message: message)
end