Skip to main content

SKY-L042 · Logic and resilience

Repeated test assertion

Low

Two identical assert statements next to each other in a test, checking a plain local value that cannot have changed in between. The second one can only pass if the first did, so it adds nothing.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

The copy usually replaced a check that was meant to be different, such as the cart total after the count. The test looks thorough while one of its intended checks is missing.

What Skylos flags

tests/test_cart.py · PythonReported on line 10
from shop.cart import Cart  def test_cart_holds_added_items():    cart = Cart()    cart.add("apple")    cart.add("pear")    count = len(cart.items)    assert count == 2    assert count == 2

What Skylos leaves alone

tests/test_cart.py · Python
from shop.cart import Cart  def test_cart_total_is_stable():    cart = Cart()    cart.add("apple")    cart.add("pear")    assert cart.total() == 5    assert cart.total() == 5

Why Skylos leaves it alone

The repeated line calls cart.total() again, and the second call can return something else if the first one changed the cart. Checking that it does not is a real test. Skylos only reports repeats of plain local values it can prove unchanged.

How Skylos decides

  1. Runs with --quality on Python test files that pytest would collect (test_*.py and *_test.py by default) and on proven unittest.TestCase classes. Skipped and xfail tests are left out, and so are tests that use global or nonlocal.
  2. Reports an assert that is identical to the statement right before it, when its condition uses at least one name and only compares values with ==, !=, is, < and similar, or combines such comparisons.
  3. Every name must be a local known to hold a plain value: a string, number, bool, None or tuple literal, or the result of the builtin len(). Calls, attributes and subscripts are not proven.
  4. Any statement between the two asserts, or a call in an assignment, resets what is known. Reports LOW on the second assert.

How to fix it

Replace the copy with the check you meant, here the cart total.

tests/test_cart.py · Python · the fix, against the flagged file
from shop.cart import Cart  def test_cart_holds_added_items():    cart = Cart()    cart.add("apple")    cart.add("pear")    count = len(cart.items)    assert count == 2Removed:     assert count == 2Added:     assert cart.total() == 5

When it can be wrong

  • It misses the same copy-paste on an attribute or call, such as two assert cart.count == 2 lines, because Skylos cannot prove the value did not change.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the second assert.

    Python
    assert count == 2  # skylos: ignore[SKY-L042] kept for the bisect script
  • [tool.skylos] ignore = ["SKY-L042"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.