circular-expectation

A test computes the value it expects.

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

Why

One side of the assertion computes its value with arithmetic, concatenation, or formatting over values the test holds, so the test can make the same mistake as the code it checks. A computation over constants alone is a stated value.

The fix, as Ur suggests it: state the expected value as a literal.

For agents

State the value a test expects as a literal rather than computing it, so the test cannot share the code's mistake.

Example

shop/tax.py
def with_tax(subtotal, rate):
    return subtotal + subtotal * rate
tests/test_tax.py
from shop.tax import with_tax


def test_adds_tax():
    subtotal = 80
    rate = 0.25
    assert with_tax(subtotal, rate) == subtotal + subtotal * rate
What Ur prints
$ ur lint --rule circular-expectation
note[circular-expectation]: the value `assert` expects is computed in test `test_adds_tax`; state it instead
  --> ./tests/test_tax.py:7:40 test `test_adds_tax`
  = hint: state the expected value as a literal
  = see: Testing on the Toilet: Don't Put Logic in Tests, Google Testing Blog (2014-07-31), https://testing.googleblog.com/2014/07/testing-on-toilet-dont-put-logic-in.html
0 clone classes and 1 test finding in 2 files

Try it in the playground →

Source

Testing on the Toilet: Don't Put Logic in Tests, Google Testing Blog (2014-07-31).