logic-in-test

Logic in a test hides what it checks.

Group
Tests
Runs
On by default
Level
note
Reads
Tests
Fix
None: whether a skipped path should check, and what the whole expected value is, are the author's to say
Defends
Precision: when the test fails, its failure says where to look.

Why

A conditional that checks on some paths and not others lets the test pass without checking, and one whose only check is to fail is an assertion written by hand. A loop that walks the value under test beside an expected sequence, asserting one pair at a time, compares two sequences one assertion could compare whole. A loop over a table of cases, a loop that builds a fixture, and the control flow of a test double or a fixture's callback are not reported.

The fix, as Ur suggests it: make the check unconditional, or compare the whole collection with one assertion.

For agents

Make every check in a test run on every path, assert conditions rather than panicking under an if, and compare whole collections with one assertion rather than element by element in a loop.

Example

shop/coupons.py
from dataclasses import dataclass


@dataclass
class Coupon:
    code: str
    percent: int


def find_coupon(code, coupons):
    for coupon in coupons:
        if coupon.code == code:
            return coupon
    return None
tests/test_coupons.py
from shop.coupons import Coupon, find_coupon


def test_finds_coupon_by_code():
    coupons = [Coupon("SPRING10", 10), Coupon("WELCOME5", 5)]
    coupon = find_coupon("WELCOME5", coupons)
    if coupon:
        assert coupon.percent == 5
What Ur prints
$ ur lint --rule logic-in-test
note[logic-in-test]: conditional checks on some paths and not others, so the test can pass without checking, in test `test_finds_coupon_by_code`
  --> ./tests/test_coupons.py:7:5-8 test `test_finds_coupon_by_code`
  = hint: make the check unconditional, or compare the whole collection with one assertion
  = 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).