2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
Assertions are meant to detect when code behaves as expected. An assertion which fails or succeeds all the time should be fixed.
This rule raises an issue when an assertion method is given parameters which will make it succeed or fail all the time. It covers three cases:
* an ``++assert++`` statement or a unittest's ``++assertTrue++`` or ``++assertFalse++`` method is called with a value which will be always True or always False.
* a unittest's ``++assertIsNotNone++`` or ``++assertIsNone++`` method is called with a value which will be always None or never None.
* a unittest's ``++assertIsNot++`` or ``++assertIs++`` method is called with a literal expression creating a new object every time (ex: ``++[1, 2, 3]++``).
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
import unittest
class MyTestCase(unittest.TestCase):
def expect_fail1(self):
assert False
def expect_fail2(self):
self.assertTrue(False) # Noncompliant. This assertion always fails.
def expect_not_none(self):
self.assertIsNotNone(round(1.5)) # Noncompliant. This assertion always succeeds because "round" returns a number, not None.
def helper_compare(param):
self.assertIs(param, [1, 2, 3]) # Noncompliant. This assertion always fails because [1, 2, 3] creates a new object.
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
import unittest
class MyTestCase(unittest.TestCase):
def expect_fail(self):
self.fail("This is expected")
def expect_not_none(self):
self.assertNotEqual(round(1.5), 0)
def helper_compare(param):
self.assertEqual(param, [1, 2, 3])
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
* https://docs.python.org/3/library/unittest.html[Python documentation - the ``++unittest++`` module]
* https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement[Python documentation - the ``++assert++`` statement]
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
for assertTrue/assertFalse
* Replace this expression; its boolean value is constant.
for assertIsNone, assertIsNotNone
* Remove this identity assertion; it will always fail.
for assertIs, assertIsNot
* Primary: Replace this "assertIs" call with an "assertEqual" call.
* Secondary: This expression creates a new object every time.
=== Highlighting
for assertTrue/assertFalse: the argument
for assertIsNone, assertIsNotNone: the whole assertion
for assertIs, assertIsNot:
* primary: the assertion method
* secondary: the expression creating a new object every time
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== relates to: S5727
=== relates to: S5796
=== relates to: S5797
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]