vacuous-assertion

An assertion that cannot fail.

Group
Tests
Runs
On by default
Level
warning
Reads
Tests
Fix
None: what the assertion should have checked is the author's choice
Defends
Fidelity: when the code is broken, the test fails.

Why

The assertion passes whatever the code under test does: it is given a literal that is always true, compares a value with itself, checks that a length is not negative, reads a mistyped mock assertion, passes its comparison as an argument of the call it meant to compare, takes the value it meant to compare with as its message, or makes an expectation without calling a matcher on it.

The fix, as Ur suggests it: assert on the result of the code under test, compared with the value it should have.

For agents

Make every assertion able to fail: compare the result of the code under test with a value stated separately, and call the matcher or assertion method rather than only naming it.

Example

shop/cart.py
class Cart:
    def __init__(self):
        self.prices = []

    def add(self, name, price):
        self.prices.append(price)

    def total(self):
        return sum(self.prices)
tests/test_cart.py
from shop.cart import Cart


def test_total_adds_every_item():
    cart = Cart()
    cart.add("kettle", 24)
    cart.add("mug", 8)
    assert (cart.total() == 32, "total should add every item")
What Ur prints
$ ur lint --rule vacuous-assertion
warning[vacuous-assertion]: `assert` is given a non-empty tuple, which is always true, so it cannot fail
  --> ./tests/test_cart.py:8:5 test `test_total_adds_every_item`
  = hint: assert on the result of the code under test, compared with the value it should have
  = see: Testing on the Toilet: Effective Testing, Google Testing Blog (2014-05-07), https://testing.googleblog.com/2014/05/testing-on-toilet-effective-testing.html
0 clone classes and 1 test finding in 2 files

Try it in the playground →

Source

Testing on the Toilet: Effective Testing, Google Testing Blog (2014-05-07).