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, what each case is called, 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, one that checks on every path chooses what the test checks, and one whose only check is to fail is an assertion written by hand. A loop that asserts checks a case for each turn that the test does not write: over a table of cases, write each case out or parametrize the test, and where it walks the value under test beside an expected sequence, compare the two whole. 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, write each case out or parametrize the test, or compare the whole collection with one assertion.
For agents
Write a test as straight-line code: no loop that asserts and no conditional around an assertion. Write each case out or parametrize the test, and compare whole collections with one assertion.
Example
shop/coupons.pyfrom 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.pyfrom 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
$ 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, write each case out or parametrize the test, 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
Source
Testing on the Toilet: Don't Put Logic in Tests, Google Testing Blog (2014-07-31).