rspec/rules/S5906/python/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

127 lines
3.3 KiB
Plaintext

== Why is this an issue?
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
[source,python]
----
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
[source,python]
----
class SomeTest(unittest.TestCase):
def test_something(self):
x = foo()
y = bar()
self.assertNotEqual(x, y)
self.assertLess(x, y)
----
== Resources
https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertEqual[Python documentation - the ``++unittest++`` module]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Default: Consider using "XXX" instead.
On assertAlmostEqual with rounded argument: Consider using the "places" argument of "assertAlmostEqual" instead.
=== Highlighting
Primary:
* assertXXX call.
* round() argument of assertAlmostEqual.
Secondary:
* argument of assertXXX call.
* assertAlmostEqual call when it's called with rounded argument.
endif::env-github,rspecator-view[]