opaque-test
A test asserts but writes no value of its own.
- Group
- Tests
- Runs
- On by default
- Level
- note
- Reads
- Tests
- Fix
- None: the values to write are the author's choice
- Defends
- Precision: when the test fails, its failure says where to look.
Why
Every value the test uses comes from a fixture, a parameter, or a helper, so nothing it checks can be read from it, and a reader cannot tell whether the test is right.
The fix, as Ur suggests it: write the values the test checks in the test.
For agents
Write the values a test checks in the test itself, so it can be verified by reading it.
Example
shop/cart.pyclass Cart:
def __init__(self, prices):
self.prices = prices
def total(self):
return sum(self.prices)
tests/conftest.pyimport pytest
from shop.cart import Cart
@pytest.fixture
def cart():
return Cart([24, 8])
@pytest.fixture
def expected_total():
return 32
tests/test_cart.pydef test_total(cart, expected_total):
assert cart.total() == expected_total
$ ur lint --rule opaque-test
note[opaque-test]: test `test_total` asserts on values it does not write, so it cannot be checked by reading it
--> ./tests/test_cart.py:1:1-2 test `test_total`
= hint: write the values the test checks in the test
= see: Testing on the Toilet: Tests Too DRY? Make Them DAMP!, Google Testing Blog (2019-12-03), https://testing.googleblog.com/2019/12/testing-on-toilet-tests-too-dry-make.html
0 clone classes and 1 test finding in 3 files
Source
Testing on the Toilet: Tests Too DRY? Make Them DAMP!, Google Testing Blog (2019-12-03).