overloaded-test

A test checks one scenario, then sets up and checks an unrelated one.

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 checking one subject, the test sets up another from scratch, with nothing from the first, and exercises it in a way it had not yet, so it covers two scenarios and its name and failure describe only one of them.

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 scenario it is about; put an independent scenario and its checks in a test of its own.

Example

shop/basket.py
class 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.py
from 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"]
What Ur prints
$ 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

Try it in the playground →

Source

Testing on the Toilet: Keep Tests Focused, Google Testing Blog (2018-06-11).