Minitest
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?)
.
Minitest/AssertEmptyLiteral
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Enabled |
Yes |
Always |
0.5 |
0.11 |
Enforces the test to use assert_empty
instead of using assert_equal([], object)
or assert_equal({}, object)
.
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)
.
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.
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))
.
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))
.
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))
.
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))
.
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
# 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')
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)
.
Minitest/AssertOutput
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
No |
0.10 |
- |
Checks for opportunities to use assert_output
.
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))
.
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?)
.
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.
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.
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))
.
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))
.
Use assert_same only when there is a need to compare by identity.
Otherwise, use assert_equal .
|
Examples
# bad
assert(expected.equal?(actual))
assert_equal(expected.object_id, actual.object_id)
# good
assert_same(expected, actual)
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('', '') { … }
.
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
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.
assert_equal(true, 'truthy') # failure
assert('truthy') # success
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
.
The second argument to the assert method named message and msg is allowed.
Because their names are inferred as message arguments.
|
Safety
This cop is unsafe because it is not possible to determine
whether the second argument of assert
is a message or not.
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.
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
# 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
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
EnforcedStyle: any (default)
# 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: _
# 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
# 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
# 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
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
# 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
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
.
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.
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.
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.
This cop assumes that classes whose superclass name includes the word “Test” are test classes, in order to prevent false positives. |
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
# 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
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.
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?)
.
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)
.
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
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.
assert_equal(false, nil) # failure
refute(nil) # success
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.
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))
.
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))
.
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))
.
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))
.
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
# 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')
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)
.
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))
.
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?)
.
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))
.
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))
.
Use refute_same only when there is a need to compare by identity.
Otherwise, use refute_equal .
|
Examples
# bad
refute(expected.equal?(actual))
refute_equal(expected.object_id, actual.object_id)
# good
refute_same(expected, actual)
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.
Minitest/SkipEnsure
Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed |
---|---|---|---|---|
Pending |
Yes |
No |
0.20 |
0.26 |
Checks that ensure
call even if skip
. It is unexpected that ensure
will be called when skipping test.
If conditional skip
is used, it checks that ensure
is also called conditionally.
On the other hand, it accepts skip
used in rescue
because ensure
may be teardown process to begin
setup process.
Examples
# 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
if 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
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.
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_
.
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.
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
.