The fixture provides shared data, and parametrize turns the business table into tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # ====================================================================== # Q: Show me pytest with fixtures and parametrize for a pricing rule. # # The fixture provides shared data, and parametrize turns the business table # into tests. I often take the table of examples from the product owner as is: # it also becomes documentation of the rule. # ====================================================================== import pytest from decimal import Decimal as D from pricing import Line, order_total @pytest.fixture def lines(): return [Line(D('10.00'), 3), Line(D('2.50'), 4)] # 30 + 10 = 40 @pytest.mark.parametrize('discount, expected', [ (D('0'), D('40.00')), (D('0.10'), D('36.00')), (D('0.333'), D('26.68')), # 26.68 after ROUND_HALF_UP (D('1'), D('0.00')), ], ids=['none', '10pct', 'odd', 'full']) def test_order_total(lines, discount, expected): assert order_total(lines, discount) == expected def test_empty_cart_is_zero(): assert order_total([], D('0.1')) == D('0.00') |