This rule raises an issue when the actual expression of an assertion matches the expected expression, meaning the same expression is being compared to itself.
Assertions that compare the same expression to itself provide no meaningful validation. They usually indicate a copy-paste mistake where the intended expected value was replaced by the actual one.
Such assertions will always pass, or always fail for inequality checks, regardless of the code’s correctness. As a result, the test gives false confidence while verifying nothing useful.
import unittest
class TestCalculation(unittest.TestCase):
def test_addition(self):
result = 2 + 2
self.assertEqual(result, result) # Noncompliant
For identity checks using assertIs, ensure you’re comparing against a different expected object, not the same one.
import unittest
class TestIdentity(unittest.TestCase):
def test_singleton(self):
obj = get_singleton()
self.assertIs(obj, obj) # Noncompliant
Replace the duplicate argument with the intended expected value.
import unittest
class TestCalculation(unittest.TestCase):
def test_addition(self):
result = 2 + 2
expected = 4
self.assertEqual(result, expected)
For identity checks using assertIs, compare against a different expected object.
import unittest
class TestIdentity(unittest.TestCase):
def test_singleton(self):
obj1 = get_singleton()
obj2 = get_singleton()
self.assertIs(obj1, obj2)
Replace the duplicate argument with the intended expected value.
def test_string_conversion():
actual = str(42)
assert actual == actual # Noncompliant
def test_string_conversion():
actual = str(42)
expected = "42"
assert actual == expected
Replace the duplicate argument with the intended expected value.
from assertpy import assert_that
def test_string_conversion():
actual = str(42)
assert_that(actual).is_equal_to(actual) # Noncompliant
from assertpy import assert_that
def test_string_conversion():
actual = str(42)
expected = "42"
assert_that(actual).is_equal_to(expected)