recomputed-in-message

A failure message calls the code under test again.

Group
Tests
Runs
On by default
Level
note
Reads
Tests
Fix
None: the variable's name is the author's to choose
Defends
Precision: when the test fails, its failure says where to look.

Why

The assertion's message makes the same call as the value it checks, so the code under test runs twice, and the message may show another value than the one that failed.

The fix, as Ur suggests it: store the result in a variable and use it in the assertion and the message.

For agents

Store the result of the code under test in a variable, and use that variable in both the assertion and its message.

Example

shop/stock.py
LEVELS = {"kettle": 3, "teapot": 0}


def in_stock(name):
    return LEVELS.get(name, 0) > 0
tests/test_stock.py
import unittest

from shop.stock import in_stock


class StockTest(unittest.TestCase):
    def test_kettle_is_in_stock(self):
        self.assertTrue(in_stock("kettle"), f"got {in_stock('kettle')}")
What Ur prints
$ ur lint --rule recomputed-in-message
note[recomputed-in-message]: the message of `assertTrue` in test `test_kettle_is_in_stock` calls the code it checks again, so it may show another value than the one that failed
  --> ./tests/test_stock.py:8:9 test `test_kettle_is_in_stock`
  = hint: store the result in a variable and use it in the assertion and the message
  = see: Why Are There So Many C++ Testing Frameworks?, Google Testing Blog (2012-10-26), https://testing.googleblog.com/2012/10/why-are-there-so-many-c-testing.html
0 clone classes and 1 test finding in 2 files

Try it in the playground →

Source

Why Are There So Many C++ Testing Frameworks?, Google Testing Blog (2012-10-26).