uninformative-assertion
An assertion is given only the result of a comparison.
- Group
- Tests
- Runs
- On by default
- Level
- note
- Reads
- Tests
- Fix
- None: the comparing assertion takes the two sides as separate arguments, which a rewrite of the call does not yet produce
- Defends
- Precision: when the test fails, its failure says where to look.
Why
The assertion receives true or false, so its failure can only say that it was false, where an assertion given both values would show what each one was. Only a comparison the framework has a comparing assertion for is reported, and the finding names it: assert!(a == b) is assert_eq!, but assert!(a < b) has no better spelling. pytest's assert rewrites the comparison, and a message that prints both values already shows them, so neither is reported.
The fix, as Ur suggests it: pass both values to the comparing assertion the finding names.
For agents
Pass the two values to an assertion that compares them, such as assert_eq! or assertEquals, rather than asserting that a comparison holds.
Example
shop/invoice.pyclass Invoice:
def __init__(self, subtotal):
self.subtotal = subtotal
def total(self, tax_rate):
return round(self.subtotal * (1 + tax_rate), 2)
tests/test_invoice.pyimport unittest
from shop.invoice import Invoice
class InvoiceTest(unittest.TestCase):
def test_adds_tax(self):
invoice = Invoice(subtotal=80)
self.assertTrue(invoice.total(tax_rate=0.25) == 100)
$ ur lint --rule uninformative-assertion
note[uninformative-assertion]: `assertTrue` is given the result of a comparison, so its failure cannot show the values compared; `assertEqual` would
--> ./tests/test_invoice.py:9:9 test `test_adds_tax`
= hint: pass both values to the comparing assertion the finding names
= see: Test Failures Should Be Actionable, Google Testing Blog (2024-05-06), https://testing.googleblog.com/2024/05/test-failures-should-be-actionable.html
0 clone classes and 1 test finding in 2 files
Source
Test Failures Should Be Actionable, Google Testing Blog (2024-05-06).