The unittest module provides assertion methods specific to common types and operations. Both versions will test the same things, but
the dedicated one will provide a better error message, simplifying the debugging process.
This rule reports an issue when an assertion can be simplified by using a more specific function. The array below gives a list of assertions on
which an issue will be raised, and which function should be used instead:
| Original |
Dedicated |
assertTrue(x == y)
|
assertEqual(x, y)
|
assertTrue(x != y)
|
assertNotEqual(x, y)
|
assertFalse(x == y)
|
assertNotEqual(x, y)
|
assertFalse(x != y)
|
assertEqual(x, y)
|
assertTrue(x < y)
|
assertLess(x, y)
|
assertTrue(x <= y)
|
assertLessEqual(x, y)
|
assertTrue(x > y)
|
assertGreater(x, y)
|
assertTrue(x >= y)
|
assertGreaterEqual(x, y)
|
assertTrue(x is y)
|
assertIs(x, y)
|
assertTrue(x is not y)
|
assertIsNot(x, y)
|
assertFalse(x is y)
|
assertIsNot(x, y)
|
assertFalse(x is not y)
|
assertIs(x, y)
|
assertTrue(x in y)
|
assertIn(x, y)
|
assertFalse(x in y)
|
assertNotIn(x, y)
|
assertTrue(isinstance(x, y))
|
assertIsInstance(x, y)
|
assertFalse(isinstance(x, y))
|
assertNotIsInstance(x, y)
|
assertEqual(x, round(y, z))
|
assertAlmostEqual(x, y, z)
|
assertAlmostEqual(x, round(y, z))
|
assertAlmostEqual(x, y, z)
|
assertNotEqual(x, round(y, z))
|
assertNotAlmostEqual(x, y, z)
|
assertNotAlmostEqual(x, round(y, z))
|
assertNotAlmostEqual(x, y, z)
|
assertEqual(x, None)
|
assertIsNone(x)
|
assertNotEqual(x, None)
|
assertIsNotNone(x)
|
assertTrue(x is None)
|
assertIsNone(x)
|
assertTrue(x is not None)
|
assertIsNotNone(x)
|
assertFalse(x is None)
|
assertIsNotNone(x)
|
assertFalse(x is not None)
|
assertIsNone(x)
|
assertEqual(x, True)
|
assertTrue(x)
|
assertEqual(x, False)
|
assertFalse(x)
|
Noncompliant code example
import unittest
class SomeTest(unittest.TestCase):
def test_something(self):
x = foo()
y = bar()
self.assertFalse(x == y) # Noncompliant
self.assertTrue(x < y) # Noncompliant
Compliant solution
class SomeTest(unittest.TestCase):
def test_something(self):
x = foo()
y = bar()
self.assertNotEqual(x, y)
self.assertLess(x, y)