This rule raises an issue on each assertion whose argument order deviates from the convention used by the rest of the test file.

Why is this an issue?

Assertions check that the code under test produces the expected results. When you write an equality assertion, one operand is the value from the code under test (the actual value) and the other is what you expect (the expected value).

Python testing frameworks commonly recommend putting the expected value second (the actual value first):

This rule does not enforce that framework default. It does not raise when a file consistently uses a different order, including when every assertion puts the expected value first. It raises only when the same file mixes both orders: readers must then re-interpret each assertion, and failure messages become harder to scan.

When a file mixes both orders, the order used by the most assertions becomes the file convention, and the rule raises one issue on each assertion that deviates from it. If both orders are used equally often, the rule takes the framework default of putting the actual value first as the convention, so it reports the assertions that put the expected value first.

A true positive is an assertion that deviates from the file convention in a file that mixes both orders. Using an order other than the framework’s documented default is fine as long as the file is consistent. Mid-migration inconsistency is still a true positive; finish unifying or temporarily suppress the issues until the file is consistent. A false positive would mean the analyzer misclassified an operand (for example, treating a non-constant expression as expected, or the reverse), so the reported "mix" is not real.

What is the potential impact?

Mixing conventions does not change whether tests pass or fail. It does make failures harder to read and slows down reviews, because each assertion must be interpreted individually instead of following one file-wide pattern.

For example, if one test writes self.assertEqual(runner.exit_code, 0) and another in the same file writes self.assertEqual(0, runner.exit_code), both may be correct in isolation, but the file no longer has a single readable convention.

How to fix it

Swap the operands of the reported assertion so it follows the same convention as the rest of the file. Alternatively, if you prefer the order used by the reported assertion, swap the other assertions instead so the whole file settles on that order.

A quick fix is available to swap the two operands of the reported assertion.

Code examples

Noncompliant code example

import unittest

class TestRunner(unittest.TestCase):
    def test_exit_code(self):
        runner = Runner()
        self.assertEqual(0, runner.exit_code)  # Noncompliant; the rest of the file puts the actual value first
        self.assertEqual(runner.name, "ok")

Compliant solution

import unittest

class TestRunner(unittest.TestCase):
    def test_exit_code(self):
        runner = Runner()
        self.assertEqual(runner.exit_code, 0)
        self.assertEqual(runner.name, "ok")

Noncompliant code example

import pytest

EXPECTED_PI = 3.14

def test_calculation():
    result = perform_calculation()
    assert 3.14 == result  # Noncompliant; the rest of the file puts the actual value first
    assert result == pytest.approx(EXPECTED_PI)

Compliant solution

import pytest

EXPECTED_PI = 3.14

def test_calculation():
    result = perform_calculation()
    assert result == 3.14
    assert result == pytest.approx(EXPECTED_PI)

Noncompliant code example

from assertpy import assert_that

EXPECTED_EXIT_CODE = 0

def test_exit_code():
    runner = Runner()
    assert_that(EXPECTED_EXIT_CODE).is_equal_to(runner.exit_code)  # Noncompliant; the rest of the file puts the actual value first
    assert_that(runner.name).is_equal_to("ok")

Compliant solution

from assertpy import assert_that

EXPECTED_EXIT_CODE = 0

def test_exit_code():
    runner = Runner()
    assert_that(runner.exit_code).is_equal_to(EXPECTED_EXIT_CODE)
    assert_that(runner.name).is_equal_to("ok")

Resources

Documentation