GitLab Well Architected
Cookbook navigation

Example: find the mistake at a shipping threshold

A test fails for an order worth exactly $50. The product rule is that shipping is free at $50 or more, but the implementation and test disagree at that boundary. This is a small enough example to check Duo's reasoning yourself.

Give Duo the failure and the rule

In the original prompt, use test_shipping_is_free_at_threshold as the test name and AssertionError: 5 != 0 as the error. Include this code and the product rule above as context.

import unittest


def shipping_fee(subtotal):
    """Return a dollar shipping fee for a nonnegative whole-dollar subtotal.

    This deliberately flawed example has no side effects. The intended rule is
    free shipping at $50 or more; the comparison below misses exactly $50.
    """
    return 0 if subtotal > 50 else 5


class ShippingTests(unittest.TestCase):
    def test_shipping_is_free_at_threshold(self):
        self.assertEqual(shipping_fee(50), 0)


if __name__ == "__main__":
    unittest.main()

You can also save the snippet as test_shipping.py and run python3 test_shipping.py. It uses Python's standard library and produces the failure described above.

Check which side should change

Duo should connect the change from > to >= to the agreed shipping rule. The rule includes $50, and the test expresses it correctly, so the implementation needs to change. Changing the assertion to expect a $5 charge would make the test pass while preserving the defect.

With the comparison corrected, these three cases make the intended behavior clear:

Subtotal Expected shipping fee
$49 $5
$50 $0
$51 $0

Try a failure caused by the test instead

Now tell Duo that the implementation uses >=, but another test expects free shipping at $49:

The product rule hasn't changed. A second test expects shipping_fee(49) to be 0,
and now fails with AssertionError: 5 != 0. Should we change the function again?

With the rule still at $50, the expectation of free shipping at $49 is wrong. Comparing each failure with the intended behavior explains why the same assertion message calls for a different fix here.

Back to the prompt