overloaded-test
A test checks one scenario, then starts and checks another.
- Group
- Tests
- Runs
- On by default
- Level
- note
- Reads
- Tests
- Fix
- None: the new test's name describes the later scenario, which the author chooses
- Defends
- Precision: when the test fails, its failure says where to look.
Why
After asserting on the outcome of one call to the code under test, the test calls it again and asserts on that, or sets up another subject from scratch and checks it, so it covers two scenarios and its name and failure describe only one of them. A check before the first call is a precondition, a call after the last check is cleanup, and a call inside an assertion is read as a reading of the outcome.
The fix, as Ur suggests it: move the later scenario and its checks into a test of their own.
For agents
End a test after checking the outcome of the call it is about; put each later call and its checks in a test of its own, with the setup they share in a helper.
Example
shop/basket.pyclass Cart:
def __init__(self):
self.prices = []
def add(self, name, price):
self.prices.append(price)
def total(self):
return sum(self.prices)
class Wishlist:
def __init__(self):
self.names = []
def save(self, name):
self.names.append(name)
def items(self):
return list(self.names)
tests/test_basket.pyfrom shop.basket import Cart, Wishlist
def test_cart_and_wishlist():
cart = Cart()
cart.add("kettle", 24)
assert cart.total() == 24
wishlist = Wishlist()
wishlist.save("teapot")
assert wishlist.items() == ["teapot"]
$ ur lint --rule overloaded-test
note[overloaded-test]: test `test_cart_and_wishlist` sets up `wishlist` afresh after checking an earlier subject, starting a second scenario
--> ./tests/test_basket.py:8:5 test `test_cart_and_wishlist`
= step 1: ./tests/test_basket.py:7:5 checks one scenario
= step 2: ./tests/test_basket.py:8:5 then sets up `wishlist` with nothing from it
= hint: move the later scenario and its checks into a test of their own
= see: Testing on the Toilet: Keep Tests Focused, Google Testing Blog (2018-06-11), https://testing.googleblog.com/2018/06/testing-on-toilet-keep-tests-focused.html
0 clone classes and 1 test finding in 2 files
Source
Testing on the Toilet: Keep Tests Focused, Google Testing Blog (2018-06-11).