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.

Why is this an issue?

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.

How to fix it in unittest

Code examples

Noncompliant code example

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

Compliant solution

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)

How to fix it in pytest

Replace the duplicate argument with the intended expected value.

Code examples

Noncompliant code example

def test_string_conversion():
    actual = str(42)
    assert actual == actual  # Noncompliant

Compliant solution

def test_string_conversion():
    actual = str(42)
    expected = "42"
    assert actual == expected

How to fix it in assertpy

Replace the duplicate argument with the intended expected value.

Code examples

Noncompliant code example

from assertpy import assert_that

def test_string_conversion():
    actual = str(42)
    assert_that(actual).is_equal_to(actual)  # Noncompliant

Compliant solution

from assertpy import assert_that

def test_string_conversion():
    actual = str(42)
    expected = "42"
    assert_that(actual).is_equal_to(expected)

Resources

Documentation