tautological-assertion
An assertion compares a value with itself.
- Group
- Tests
- Runs
- On by default
- Level
- warning
- Reads
- Tests
- Fix
- None: what the test should expect is the author's choice
- Defends
- Fidelity: when the code is broken, the test fails.
Why
Both sides of the assertion are the same term, once each local the test assigns once is replaced by its value, so the assertion cannot fail. Ur does not yet know a value's type, so a test of a type's own ==, such as assert_eq!(x, x), is reported too; mark one written on purpose with ur-allow. A variable compared with itself by identity is left to vacuous-assertion.
The fix, as Ur suggests it: compare with a value stated independently of the code.
For agents
Compare the result of the code under test with a value stated independently of it, never with the same expression.
Example
shop/cart.pyclass Cart:
def __init__(self, prices):
self.prices = prices
def total(self):
return sum(self.prices)
tests/test_cart.pyfrom shop.cart import Cart
def test_total():
cart = Cart([24, 8])
expected = cart.total()
assert cart.total() == expected
$ ur lint --rule tautological-assertion
warning[tautological-assertion]: both sides of `assert` in test `test_total` are the same value, so it cannot fail
--> ./tests/test_cart.py:7:5 test `test_total`
= hint: compare with a value stated independently of the code
= 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
Source
Testing on the Toilet: Effective Testing, Google Testing Blog (2014-05-07).