////
  Do NOT edit this file by hand directly, as it is automatically generated.

  Please make any necessary changes to the cop documentation within the source files themselves.
////

= Minitest

[#minitestassertempty]
== Minitest/AssertEmpty

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.2
| -
|===

Enforces the test to use `assert_empty` instead of using `assert(object.empty?)`.

[#examples-minitestassertempty]
=== Examples

[source,ruby]
----
# bad
assert(object.empty?)
assert(object.empty?, 'message')

# good
assert_empty(object)
assert_empty(object, 'message')
----

[#references-minitestassertempty]
=== References

* https://minitest.rubystyle.guide#assert-empty

[#minitestassertemptyliteral]
== Minitest/AssertEmptyLiteral

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| Always
| 0.5
| 0.39
|===

Enforces the test to use `assert_empty`
instead of using `assert_equal([], object)` or `assert_equal({}, object)`.

NOTE: Using `assert_empty` makes it impossible to indicate that the expected value is
the literal `[]` or `{}`. Since this removes the ability to express the difference
between those literals, it is disabled by default in consideration of that drawback.
Since the role of replacing the originally intended `assert([], empty_list)` with
`assert_equal([], empty_list)` is handled by `Minitest/AssertWithExpectedArgumentTest`,
this cop may be removed in the future.

[#examples-minitestassertemptyliteral]
=== Examples

[source,ruby]
----
# bad
assert_equal([], object)
assert_equal({}, object)

# good
assert_empty(object)
----

[#minitestassertequal]
== Minitest/AssertEqual

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.4
| -
|===

Enforces the use of `assert_equal(expected, actual)`
over `assert(expected == actual)`.

[#examples-minitestassertequal]
=== Examples

[source,ruby]
----
# bad
assert("rubocop-minitest" == actual)
assert_operator("rubocop-minitest", :==, actual)

# good
assert_equal("rubocop-minitest", actual)
----

[#references-minitestassertequal]
=== References

* https://minitest.rubystyle.guide#assert-equal-arguments-order

[#minitestassertindelta]
== Minitest/AssertInDelta

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| -
|===

Enforces the test to use `assert_in_delta`
instead of using `assert_equal` to compare floats.

[#examples-minitestassertindelta]
=== Examples

[source,ruby]
----
# bad
assert_equal(0.2, actual)
assert_equal(0.2, actual, 'message')

# good
assert_in_delta(0.2, actual)
assert_in_delta(0.2, actual, 0.001, 'message')
----

[#references-minitestassertindelta]
=== References

* https://minitest.rubystyle.guide/#assert-in-delta

[#minitestassertincludes]
== Minitest/AssertIncludes

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.2
| -
|===

Enforces the test to use `assert_includes`
instead of using `assert(collection.include?(object))`.

[#examples-minitestassertincludes]
=== Examples

[source,ruby]
----
# bad
assert(collection.include?(object))
assert(collection.include?(object), 'message')

# good
assert_includes(collection, object)
assert_includes(collection, object, 'message')
----

[#references-minitestassertincludes]
=== References

* https://minitest.rubystyle.guide#assert-includes

[#minitestassertinstanceof]
== Minitest/AssertInstanceOf

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.4
| -
|===

Enforces the test to use `assert_instance_of(Class, object)`
over `assert(object.instance_of?(Class))`.

[#examples-minitestassertinstanceof]
=== Examples

[source,ruby]
----
# bad
assert(object.instance_of?(Class))
assert(object.instance_of?(Class), 'message')

# bad
assert_equal(Class, object.class)
assert_equal(Class, object.class, 'message')

# good
assert_instance_of(Class, object)
assert_instance_of(Class, object, 'message')
----

[#references-minitestassertinstanceof]
=== References

* https://minitest.rubystyle.guide#assert-instance-of

[#minitestassertkindof]
== Minitest/AssertKindOf

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| 0.34
|===

Enforces the test to use `assert_kind_of(Class, object)`
over `assert(object.kind_of?(Class))`.

[#examples-minitestassertkindof]
=== Examples

[source,ruby]
----
# bad
assert(object.kind_of?(Class))
assert(object.kind_of?(Class), 'message')

# bad
# `is_a?` is an alias for `kind_of?`
assert(object.is_a?(Class))
assert(object.is_a?(Class), 'message')

# good
assert_kind_of(Class, object)
assert_kind_of(Class, object, 'message')
----

[#references-minitestassertkindof]
=== References

* https://github.com/rubocop/minitest-style-guide#assert-kind-of

[#minitestassertmatch]
== Minitest/AssertMatch

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.6
| -
|===

Enforces the test to use `assert_match`
instead of using `assert(matcher.match(string))`.

[#examples-minitestassertmatch]
=== Examples

[source,ruby]
----
# bad
assert(matcher.match(string))
assert(matcher.match?(string))
assert(matcher =~ string)
assert_operator(matcher, :=~, string)
assert(matcher.match(string), 'message')

# good
assert_match(regex, string)
assert_match(matcher, string, 'message')
----

[#references-minitestassertmatch]
=== References

* https://minitest.rubystyle.guide#assert-match

[#minitestassertnil]
== Minitest/AssertNil

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.1
| -
|===

Enforces the test to use `assert_nil` instead of using
`assert_equal(nil, something)`, `assert(something.nil?)`, or `assert_predicate(something, :nil?)`.

[#examples-minitestassertnil]
=== Examples

[source,ruby]
----
# bad
assert_equal(nil, actual)
assert_equal(nil, actual, 'message')
assert(object.nil?)
assert(object.nil?, 'message')
assert_predicate(object, :nil?)
assert_predicate(object, :nil?, 'message')

# good
assert_nil(actual)
assert_nil(actual, 'message')
----

[#references-minitestassertnil]
=== References

* https://minitest.rubystyle.guide#assert-nil

[#minitestassertoperator]
== Minitest/AssertOperator

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.32
| -
|===

Enforces the use of `assert_operator(expected, :<, actual)` over `assert(expected < actual)`.

[#examples-minitestassertoperator]
=== Examples

[source,ruby]
----
# bad
assert(expected < actual)

# good
assert_operator(expected, :<, actual)
----

[#references-minitestassertoperator]
=== References

* https://minitest.rubystyle.guide#assert-operator

[#minitestassertoutput]
== Minitest/AssertOutput

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.10
| -
|===

Checks for opportunities to use `assert_output`.

[#examples-minitestassertoutput]
=== Examples

[source,ruby]
----
# bad
$stdout = StringIO.new
puts object.method
$stdout.rewind
assert_match expected, $stdout.read

# good
assert_output(expected) { puts object.method }
----

[#references-minitestassertoutput]
=== References

* https://minitest.rubystyle.guide/#assert-output

[#minitestassertpathexists]
== Minitest/AssertPathExists

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| -
|===

Enforces the test to use `assert_path_exists` instead of using `assert(File.exist?(path))`.

[#examples-minitestassertpathexists]
=== Examples

[source,ruby]
----
# bad
assert(File.exist?(path))
assert(File.exist?(path), 'message')

# good
assert_path_exists(path)
assert_path_exists(path, 'message')
----

[#references-minitestassertpathexists]
=== References

* https://minitest.rubystyle.guide/#assert-path-exists

[#minitestassertpredicate]
== Minitest/AssertPredicate

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.18
| -
|===

Enforces the test to use `assert_predicate`
instead of using `assert(obj.a_predicate_method?)`.

[#examples-minitestassertpredicate]
=== Examples

[source,ruby]
----
# bad
assert(obj.one?)
assert(obj.one?, 'message')

# good
assert_predicate(obj, :one?)
assert_predicate(obj, :one?, 'message')
----

[#references-minitestassertpredicate]
=== References

* https://minitest.rubystyle.guide/#assert-predicate

[#minitestassertraisescompoundbody]
== Minitest/AssertRaisesCompoundBody

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.21
| -
|===

Enforces the block body of `assert_raises { ... }` to be reduced to only the raising code.

[#examples-minitestassertraisescompoundbody]
=== Examples

[source,ruby]
----
# bad
assert_raises(MyError) do
  foo
  bar
end

# good
assert_raises(MyError) do
  foo
end

# good
assert_raises(MyError) do
  foo do
    bar
    baz
  end
end
----

[#minitestassertraiseswithregexpargument]
== Minitest/AssertRaisesWithRegexpArgument

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.22
| 0.26
|===

Checks for `assert_raises` with arguments of regular expression literals.
Arguments should be exception classes.
Optionally the last argument can be a custom message string to help explain failures.
Either way, it's not the argument that `exception.message` is compared to.
The raised exception is returned and can be used
to match against a regular expression.

[#examples-minitestassertraiseswithregexpargument]
=== Examples

[source,ruby]
----
# bad
assert_raises FooError, /some message/ do
  obj.occur_error
end

# good
exception = assert_raises FooError do
  obj.occur_error
end
assert_match(/some message/, exception.message)
----

[#configurable-attributes-minitestassertraiseswithregexpargument]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Severity
| `warning`
| String
|===

[#minitestassertrespondto]
== Minitest/AssertRespondTo

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.3
| -
|===

Enforces the use of `assert_respond_to(object, :do_something)`
over `assert(object.respond_to?(:do_something))`.

[#examples-minitestassertrespondto]
=== Examples

[source,ruby]
----
# bad
assert(object.respond_to?(:do_something))
assert(object.respond_to?(:do_something), 'message')
assert(respond_to?(:do_something))

# good
assert_respond_to(object, :do_something)
assert_respond_to(object, :do_something, 'message')
assert_respond_to(self, :do_something)
----

[#references-minitestassertrespondto]
=== References

* https://minitest.rubystyle.guide#assert-responds-to-method

[#minitestassertsame]
== Minitest/AssertSame

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.26
| -
|===

Enforces the use of `assert_same(expected, actual)`
over `assert(expected.equal?(actual))`.

NOTE: Use `assert_same` only when there is a need to compare by identity.
      Otherwise, use `assert_equal`.

[#examples-minitestassertsame]
=== Examples

[source,ruby]
----
# bad
assert(expected.equal?(actual))
assert_equal(expected.object_id, actual.object_id)

# good
assert_same(expected, actual)
----

[#references-minitestassertsame]
=== References

* https://minitest.rubystyle.guide#assert-same

[#minitestassertsilent]
== Minitest/AssertSilent

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| -
|===

Enforces the test to use `assert_silent { ... }`
instead of using `assert_output('', '') { ... }`.

[#examples-minitestassertsilent]
=== Examples

[source,ruby]
----
# bad
assert_output('', '') { puts object.do_something }

# good
assert_silent { puts object.do_something }
----

[#references-minitestassertsilent]
=== References

* https://github.com/rubocop/minitest-style-guide#assert-silent

[#minitestasserttruthy]
== Minitest/AssertTruthy

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| No
| Always (Unsafe)
| 0.2
| 0.27
|===

Enforces the test to use `assert(actual)` instead of using `assert_equal(true, actual)`.

[#safety-minitestasserttruthy]
=== Safety

This cop is unsafe because true might be expected instead of truthy.
False positives cannot be prevented when this is a variable or method return value.

[source,ruby]
----
assert_equal(true, 'truthy') # failure
assert('truthy')             # success
----

[#examples-minitestasserttruthy]
=== Examples

[source,ruby]
----
# bad
assert_equal(true, actual)
assert_equal(true, actual, 'message')

# good
assert(actual)
assert(actual, 'message')
----

[#references-minitestasserttruthy]
=== References

* https://minitest.rubystyle.guide#assert-truthy

[#minitestassertwithexpectedargument]
== Minitest/AssertWithExpectedArgument

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| No
| No
| 0.11
| 0.26
|===

Tries to detect when a user accidentally used
`assert` when they meant to use `assert_equal`.

NOTE: The second argument to the `assert` method named `message` and `msg` is allowed.
      Because their names are inferred as message arguments.

[#safety-minitestassertwithexpectedargument]
=== Safety

This cop is unsafe because it is not possible to determine
whether the second argument of `assert` is a message or not.

[#examples-minitestassertwithexpectedargument]
=== Examples

[source,ruby]
----
# bad
assert(3, my_list.length)
assert([], empty_list)
assert(expected, actual)

# good
assert_equal(3, my_list.length)
assert_equal([], empty_list)
assert_equal(expected, actual)
assert(foo, 'message')
assert(foo, message)
assert(foo, msg)
----

[#configurable-attributes-minitestassertwithexpectedargument]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Severity
| `warning`
| String
|===

[#minitestassertioninlifecyclehook]
== Minitest/AssertionInLifecycleHook

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.10
| -
|===

Checks for usage of assertions in lifecycle hooks.

[#examples-minitestassertioninlifecyclehook]
=== Examples

[source,ruby]
----
# bad
class FooTest < Minitest::Test
  def setup
    assert_equal(foo, bar)
  end
end

# good
class FooTest < Minitest::Test
  def test_something
    assert_equal(foo, bar)
  end
end
----

[#minitestduplicatetestrun]
== Minitest/DuplicateTestRun

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.19
| -
|===

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-minitestduplicatetestrun]
=== Examples

[source,ruby]
----
# 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
----

[#references-minitestduplicatetestrun]
=== References

* https://minitest.rubystyle.guide/#subclassing-test-cases

[#minitestemptylinebeforeassertionmethods]
== Minitest/EmptyLineBeforeAssertionMethods

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.23
| -
|===

Enforces empty line before assertion methods because it separates assertion phase.

[#examples-minitestemptylinebeforeassertionmethods]
=== Examples

[source,ruby]
----
# bad
do_something
assert_equal(expected, actual)

# good
do_something

assert_equal(expected, actual)
----

[#minitestfocus]
== Minitest/Focus

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Command-line only
| 0.35
| -
|===

Enforces tests are not focused.

[#examples-minitestfocus]
=== Examples

[source,ruby]
----
# bad
focus test 'foo' do
end

# bad
focus
test 'foo' do
end

# good
test 'foo' do
end
----

[#minitestglobalexpectations]
== Minitest/GlobalExpectations

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.7
| 0.26
|===

Checks for deprecated global expectations
and autocorrects them to use expect format.

[#examples-minitestglobalexpectations]
=== Examples

[#enforcedstyle_-any-_default_-minitestglobalexpectations]
==== EnforcedStyle: any (default)

[source,ruby]
----
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError

# good
_(musts).must_equal expected_musts
_(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError

expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError

value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError
----

[#enforcedstyle_-_-minitestglobalexpectations]
==== EnforcedStyle: _

[source,ruby]
----
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError

expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError

value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError

# good
_(musts).must_equal expected_musts
_(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError
----

[#enforcedstyle_-expect-minitestglobalexpectations]
==== EnforcedStyle: expect

[source,ruby]
----
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError

_(musts).must_equal expected_musts
_(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError

value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError

# good
expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError
----

[#enforcedstyle_-value-minitestglobalexpectations]
==== EnforcedStyle: value

[source,ruby]
----
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError

_(musts).must_equal expected_musts
_(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError

expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError

# good
value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError
----

[#configurable-attributes-minitestglobalexpectations]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Severity
| `warning`
| String

| EnforcedStyle
| `any`
| `_`, `any`, `expect`, `value`

| Include
| `+**/test/**/*+`, `+**/*_test.rb+`, `+**/spec/**/*+`, `+**/*_spec.rb+`
| Array
|===

[#references-minitestglobalexpectations]
=== References

* https://minitest.rubystyle.guide#global-expectations

[#minitestlifecyclehooksorder]
== Minitest/LifecycleHooksOrder

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.28
| -
|===

Checks that lifecycle hooks are declared in the order in which they will be executed.

[#examples-minitestlifecyclehooksorder]
=== Examples

[source,ruby]
----
# bad
class FooTest < Minitest::Test
  def teardown; end
  def setup; end
end

# good
class FooTest < Minitest::Test
  def setup; end
  def teardown; end
end

# bad (after test cases)
class FooTest < Minitest::Test
  def test_something
    assert foo
  end
  def setup; end
  def teardown; end
end

# good
class FooTest < Minitest::Test
  def setup; end
  def teardown; end
  def test_something
    assert foo
  end
end

# good (after non test case methods)
class FooTest < Minitest::Test
  def do_something; end
  def setup; end
  def teardown; end
end
----

[#references-minitestlifecyclehooksorder]
=== References

* https://minitest.rubystyle.guide/#hooks-ordering

[#minitestliteralasactualargument]
== Minitest/LiteralAsActualArgument

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| -
|===

Enforces correct order of expected and
actual arguments for `assert_equal`.

[#examples-minitestliteralasactualargument]
=== Examples

[source,ruby]
----
# bad
assert_equal foo, 2
assert_equal foo, [1, 2]
assert_equal foo, [1, 2], 'message'

# good
assert_equal 2, foo
assert_equal [1, 2], foo
assert_equal [1, 2], foo, 'message'
----

[#references-minitestliteralasactualargument]
=== References

* https://minitest.rubystyle.guide/#assert-equal-arguments-order

[#minitestmultipleassertions]
== Minitest/MultipleAssertions

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.10
| -
|===

Checks if test cases contain too many assertion calls. If conditional code with assertions
is used, the branch with maximum assertions is counted.
The maximum allowed assertion calls is configurable.

[#examples-minitestmultipleassertions]
=== Examples

[#max_-1-minitestmultipleassertions]
==== Max: 1

[source,ruby]
----
# bad
class FooTest < Minitest::Test
  def test_asserts_twice
    assert_equal(42, do_something)
    assert_empty(array)
  end
end

# good
class FooTest < Minitest::Test
  def test_asserts_once
    assert_equal(42, do_something)
  end

  def test_another_asserts_once
    assert_empty(array)
  end
end
----

[#configurable-attributes-minitestmultipleassertions]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Max
| `3`
| Integer
|===

[#minitestnoassertions]
== Minitest/NoAssertions

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| No
| 0.12
| -
|===

Checks if test cases contain any assertion calls.

Matchers such as `must_equal` and `wont_match` are also treated as assertion methods.

[#examples-minitestnoassertions]
=== Examples

[source,ruby]
----
# bad
class FooTest < Minitest::Test
  def test_the_truth
  end
end

# good
class FooTest < Minitest::Test
  def test_the_truth
    assert true
  end
end

# bad
class FooTest < ActiveSupport::TestCase
  describe 'foo' do
    it 'test equal' do
    end
  end
end

# good
class FooTest < ActiveSupport::TestCase
  describe 'foo' do
    it 'test equal' do
      musts.must_equal expected_musts
    end
  end
end
----

[#minitestnotestcases]
== Minitest/NoTestCases

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Disabled
| Yes
| No
| 0.30
| -
|===

Checks if test class contains any test cases.

[#examples-minitestnotestcases]
=== Examples

[source,ruby]
----
# bad
class FooTest < Minitest::Test
  def do_something
  end
end

# good
class FooTest < Minitest::Test
  def test_something
    assert true
  end
end
----

[#minitestnonexecutabletestmethod]
== Minitest/NonExecutableTestMethod

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.34
| -
|===

Checks for the use of test methods outside of a test class.

Test methods should be defined within a test class to ensure their execution.

NOTE: This cop assumes that classes whose superclass name includes the word
"`Test`" are test classes, in order to prevent false positives.

[#examples-minitestnonexecutabletestmethod]
=== Examples

[source,ruby]
----
# bad
class FooTest < Minitest::Test
end
def test_method_should_be_inside_test_class
end

# good
class FooTest < Minitest::Test
  def test_method_should_be_inside_test_class
  end
end
----

[#configurable-attributes-minitestnonexecutabletestmethod]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Severity
| `warning`
| String
|===

[#minitestnonpublictestmethod]
== Minitest/NonPublicTestMethod

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.27
| -
|===

Detects non `public` (marked as `private` or `protected`) test methods.
Minitest runs only test methods which are `public`.

[#examples-minitestnonpublictestmethod]
=== Examples

[source,ruby]
----
# bad
class FooTest
  private # or protected
  def test_does_something
    assert_equal 42, do_something
  end
end

# good
class FooTest
  def test_does_something
    assert_equal 42, do_something
  end
end

# good (not a test case name)
class FooTest
  private # or protected
  def does_something
    assert_equal 42, do_something
  end
end

# good (no assertions)
class FooTest
  private # or protected
  def test_does_something
    do_something
  end
end
----

[#configurable-attributes-minitestnonpublictestmethod]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Severity
| `warning`
| String
|===

[#minitestredundantmessageargument]
== Minitest/RedundantMessageArgument

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.34
| -
|===

Detects redundant message argument in assertion methods.
The message argument `nil` is redundant because it is the default value.

[#examples-minitestredundantmessageargument]
=== Examples

[source,ruby]
----
# bad
assert_equal(expected, actual, nil)

# good
assert_equal(expected, actual)
assert_equal(expected, actual, 'message')
----

[#minitestrefuteempty]
== Minitest/RefuteEmpty

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.3
| -
|===

Enforces to use `refute_empty` instead of using `refute(object.empty?)`.

[#examples-minitestrefuteempty]
=== Examples

[source,ruby]
----
# bad
refute(object.empty?)
refute(object.empty?, 'message')

# good
refute_empty(object)
refute_empty(object, 'message')
----

[#references-minitestrefuteempty]
=== References

* https://minitest.rubystyle.guide#refute-empty

[#minitestrefuteequal]
== Minitest/RefuteEqual

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.3
| -
|===

Enforces the use of `refute_equal(expected, object)`
over `assert(expected != actual)` or `assert(! expected == actual)`.

[#examples-minitestrefuteequal]
=== Examples

[source,ruby]
----
# bad
assert("rubocop-minitest" != actual)
refute("rubocop-minitest" == actual)
assert_operator("rubocop-minitest", :!=, actual)
refute_operator("rubocop-minitest", :==, actual)

# good
refute_equal("rubocop-minitest", actual)
----

[#references-minitestrefuteequal]
=== References

* https://minitest.rubystyle.guide#refute-equal

[#minitestrefutefalse]
== Minitest/RefuteFalse

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| No
| Always (Unsafe)
| 0.3
| 0.27
|===

Enforces the use of `refute(object)` over `assert_equal(false, object)`.

[#safety-minitestrefutefalse]
=== Safety

This cop is unsafe because it cannot detect failure when second argument is `nil`.
False positives cannot be prevented when this is a variable or method return value.

[source,ruby]
----
assert_equal(false, nil) # failure
refute(nil)              # success
----

[#examples-minitestrefutefalse]
=== Examples

[source,ruby]
----
# bad
assert_equal(false, actual)
assert_equal(false, actual, 'message')

assert(!test)
assert(!test, 'message')

# good
refute(actual)
refute(actual, 'message')
----

[#references-minitestrefutefalse]
=== References

* https://minitest.rubystyle.guide#refute-false

[#minitestrefuteindelta]
== Minitest/RefuteInDelta

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| -
|===

Enforces the test to use `refute_in_delta`
instead of using `refute_equal` to compare floats.

[#examples-minitestrefuteindelta]
=== Examples

[source,ruby]
----
# bad
refute_equal(0.2, actual)
refute_equal(0.2, actual, 'message')

# good
refute_in_delta(0.2, actual)
refute_in_delta(0.2, actual, 0.001, 'message')
----

[#references-minitestrefuteindelta]
=== References

* https://minitest.rubystyle.guide/#refute-in-delta

[#minitestrefuteincludes]
== Minitest/RefuteIncludes

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.3
| -
|===

Enforces the test to use `refute_includes`
instead of using `refute(collection.include?(object))`.

[#examples-minitestrefuteincludes]
=== Examples

[source,ruby]
----
# bad
refute(collection.include?(object))
refute(collection.include?(object), 'message')

# good
refute_includes(collection, object)
refute_includes(collection, object, 'message')
----

[#references-minitestrefuteincludes]
=== References

* https://minitest.rubystyle.guide#refute-includes

[#minitestrefuteinstanceof]
== Minitest/RefuteInstanceOf

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.4
| -
|===

Enforces the use of `refute_instance_of(Class, object)`
over `refute(object.instance_of?(Class))`.

[#examples-minitestrefuteinstanceof]
=== Examples

[source,ruby]
----
# bad
refute(object.instance_of?(Class))
refute(object.instance_of?(Class), 'message')

# bad
refute_equal(Class, object.class)
refute_equal(Class, object.class, 'message')

# good
refute_instance_of(Class, object)
refute_instance_of(Class, object, 'message')
----

[#references-minitestrefuteinstanceof]
=== References

* https://minitest.rubystyle.guide#refute-instance-of

[#minitestrefutekindof]
== Minitest/RefuteKindOf

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| 0.34
|===

Enforces the use of `refute_kind_of(Class, object)`
over `refute(object.kind_of?(Class))`.

[#examples-minitestrefutekindof]
=== Examples

[source,ruby]
----
# bad
refute(object.kind_of?(Class))
refute(object.kind_of?(Class), 'message')

# bad
# `is_a?` is an alias for `kind_of?`
refute(object.is_of?(Class))
refute(object.is_of?(Class), 'message')

# good
refute_kind_of(Class, object)
refute_kind_of(Class, object, 'message')
----

[#references-minitestrefutekindof]
=== References

* https://github.com/rubocop/minitest-style-guide#refute-kind-of

[#minitestrefutematch]
== Minitest/RefuteMatch

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.6
| -
|===

Enforces the test to use `refute_match`
instead of using `refute(matcher.match(string))`.

[#examples-minitestrefutematch]
=== Examples

[source,ruby]
----
# bad
refute(matcher.match(string))
refute(matcher.match?(string))
refute(matcher =~ string)
refute_operator(matcher, :=~, string)
assert_operator(matcher, :!~, string)
refute(matcher.match(string), 'message')

# good
refute_match(matcher, string)
refute_match(matcher, string, 'message')
----

[#references-minitestrefutematch]
=== References

* https://minitest.rubystyle.guide#refute-match

[#minitestrefutenil]
== Minitest/RefuteNil

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.2
| -
|===

Enforces the test to use `refute_nil` instead of using
`refute_equal(nil, something)`, `refute(something.nil?)`, or `refute_predicate(something, :nil?)`.

[#examples-minitestrefutenil]
=== Examples

[source,ruby]
----
# bad
refute_equal(nil, actual)
refute_equal(nil, actual, 'message')
refute(actual.nil?)
refute(actual.nil?, 'message')
refute_predicate(object, :nil?)
refute_predicate(object, :nil?, 'message')

# good
refute_nil(actual)
refute_nil(actual, 'message')
----

[#references-minitestrefutenil]
=== References

* https://minitest.rubystyle.guide#refute-nil

[#minitestrefuteoperator]
== Minitest/RefuteOperator

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.32
| -
|===

Enforces the use of `refute_operator(expected, :<, actual)` over `refute(expected < actual)`.

[#examples-minitestrefuteoperator]
=== Examples

[source,ruby]
----
# bad
refute(expected < actual)

# good
refute_operator(expected, :<, actual)
----

[#references-minitestrefuteoperator]
=== References

* https://minitest.rubystyle.guide#refute-operator

[#minitestrefutepathexists]
== Minitest/RefutePathExists

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| -
|===

Enforces the test to use `refute_path_exists` instead of using `refute(File.exist?(path))`.

[#examples-minitestrefutepathexists]
=== Examples

[source,ruby]
----
# bad
refute(File.exist?(path))
refute(File.exist?(path), 'message')

# good
refute_path_exists(path)
refute_path_exists(path, 'message')
----

[#references-minitestrefutepathexists]
=== References

* https://minitest.rubystyle.guide/#refute-path-exists

[#minitestrefutepredicate]
== Minitest/RefutePredicate

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.18
| -
|===

Enforces the test to use `refute_predicate`
instead of using `refute(obj.a_predicate_method?)`.

[#examples-minitestrefutepredicate]
=== Examples

[source,ruby]
----
# bad
refute(obj.one?)
refute(obj.one?, 'message')

# good
refute_predicate(obj, :one?)
refute_predicate(obj, :one?, 'message')
----

[#references-minitestrefutepredicate]
=== References

* https://minitest.rubystyle.guide/#refute-predicate

[#minitestrefuterespondto]
== Minitest/RefuteRespondTo

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Enabled
| Yes
| Always
| 0.4
| -
|===

Enforces the test to use `refute_respond_to(object, :do_something)`
over `refute(object.respond_to?(:do_something))`.

[#examples-minitestrefuterespondto]
=== Examples

[source,ruby]
----
# bad
refute(object.respond_to?(:do_something))
refute(object.respond_to?(:do_something), 'message')
refute(respond_to?(:do_something))

# good
refute_respond_to(object, :do_something)
refute_respond_to(object, :do_something, 'message')
refute_respond_to(self, :do_something)
----

[#references-minitestrefuterespondto]
=== References

* https://minitest.rubystyle.guide#refute-respond-to

[#minitestrefutesame]
== Minitest/RefuteSame

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.26
| -
|===

Enforces the use of `refute_same(expected, object)`
over `refute(expected.equal?(actual))`.

NOTE: Use `refute_same` only when there is a need to compare by identity.
      Otherwise, use `refute_equal`.

[#examples-minitestrefutesame]
=== Examples

[source,ruby]
----
# bad
refute(expected.equal?(actual))
refute_equal(expected.object_id, actual.object_id)

# good
refute_same(expected, actual)
----

[#references-minitestrefutesame]
=== References

* https://minitest.rubystyle.guide#refute-same

[#minitestreturnintestmethod]
== Minitest/ReturnInTestMethod

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.31
| -
|===

Enforces the use of `skip` instead of `return` in test methods.

[#examples-minitestreturnintestmethod]
=== Examples

[source,ruby]
----
# bad
def test_something
  return if condition?
  assert_equal(42, something)
end

# good
def test_something
  skip if condition?
  assert_equal(42, something)
end
----

[#references-minitestreturnintestmethod]
=== References

* https://minitest.rubystyle.guide/#skipping-runnable-methods

[#minitestskipensure]
== Minitest/SkipEnsure

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.20
| 0.26
|===

Checks that code in an `ensure` block does not run when the test is skipped.
If `skip` is conditional, the `ensure` block must also be conditional,
using the negation of the skip condition so that it runs only when the test runs.

On the other hand, `skip` used inside a `rescue` clause is accepted,
because the `ensure` block may serve as teardown for resources created in the `begin` setup.

[#examples-minitestskipensure]
=== Examples

[source,ruby]
----
# bad
def test_skip
  skip 'This test is skipped.'

  assert 'foo'.present?
ensure
  do_something
end

# bad
def test_conditional_skip
  skip 'This test is skipped.' if condition

  assert do_something
ensure
  do_teardown
end

# good
def test_skip
  skip 'This test is skipped.'

  begin
    assert 'foo'.present?
  ensure
    do_something
  end
end

# good
def test_conditional_skip
  skip 'This test is skipped.' if condition

  assert do_something
ensure
  unless condition
    do_teardown
  end
end

# good
def test_skip_is_used_in_rescue
  do_setup
  assert do_something
rescue
  skip 'This test is skipped.'
ensure
  do_teardown
end
----

[#configurable-attributes-minitestskipensure]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Severity
| `warning`
| String
|===

[#minitestskipwithoutreason]
== Minitest/SkipWithoutReason

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.24
| -
|===

Checks for skipped tests missing the skipping reason.

[#examples-minitestskipwithoutreason]
=== Examples

[source,ruby]
----
# bad
skip
skip('')

# bad
if condition?
  skip
else
  skip
end

# good
skip("Reason why the test was skipped")

# good
skip if condition?
----

[#minitesttestfilename]
== Minitest/TestFileName

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.26
| -
|===

Checks if test file names start with `test_` or end with `_test.rb`.
Files which define classes having names ending with `Test` are checked.
Not following this convention may result in tests not being run.

[#examples-minitesttestfilename]
=== Examples

[source,ruby]
----
# bad
my_class.rb

# good
my_class_test.rb
test_my_class.rb
----

[#references-minitesttestfilename]
=== References

* https://minitest.rubystyle.guide/#file-naming

[#minitesttestmethodname]
== Minitest/TestMethodName

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Always
| 0.10
| -
|===

Enforces that test method names start with `test_` prefix.
It aims to prevent tests that aren't executed by forgetting to start test method name with `test_`.

[#examples-minitesttestmethodname]
=== Examples

[source,ruby]
----
# bad
class FooTest < Minitest::Test
  def does_something
    assert_equal 42, do_something
  end
end

# good
class FooTest < Minitest::Test
  def test_does_something
    assert_equal 42, do_something
  end
end

# good
class FooTest < Minitest::Test
  def helper_method(argument)
  end
end
----

[#minitestunreachableassertion]
== Minitest/UnreachableAssertion

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.14
| 0.26
|===

Checks for `assert_raises` has an assertion method at
the bottom of block because the assertion will be never reached.

[#examples-minitestunreachableassertion]
=== Examples

[source,ruby]
----
# bad
assert_raises FooError do
  obj.occur_error
  assert_equal('foo', obj.bar) # Never asserted.
end

# good
assert_raises FooError do
  obj.occur_error
end
assert_equal('foo', obj.bar)
----

[#configurable-attributes-minitestunreachableassertion]
=== Configurable attributes

|===
| Name | Default value | Configurable values

| Severity
| `warning`
| String
|===

[#minitestunspecifiedexception]
== Minitest/UnspecifiedException

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.10
| -
|===

Checks for a specified error in `assert_raises`.

[#examples-minitestunspecifiedexception]
=== Examples

[source,ruby]
----
# bad
assert_raises { raise FooException }
assert_raises('This should have raised') { raise FooException }

# good
assert_raises(FooException) { raise FooException }
assert_raises(FooException, 'This should have raised') { raise FooException }
----

[#references-minitestunspecifiedexception]
=== References

* https://minitest.rubystyle.guide#unspecified-exception

[#minitestuselessassertion]
== Minitest/UselessAssertion

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| No
| 0.26
| -
|===

Detects useless assertions (assertions that either always pass or always fail).

[#examples-minitestuselessassertion]
=== Examples

[source,ruby]
----
# bad
assert true
assert_equal @foo, @foo
assert_nil [foo, bar]

# good
assert something
assert_equal foo, bar
assert_nil foo
assert false, "My message"
----
