rspec/rules/S5906/python/rule.adoc

109 lines
2.9 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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)++``|
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
import unittest
class SomeTest(unittest.TestCase):
def test_something(self):
x = foo()
y = bar()
self.assertFalse(x == y) # Noncompliant
self.assertTrue(x < y) # Noncompliant
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
class SomeTest(unittest.TestCase):
def test_something(self):
x = foo()
y = bar()
self.assertNotEqual(x, y)
self.assertLess(x, y)
----
2021-04-28 16:49:39 +02:00
== See
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)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]