Full answer
python
import io, unittest
def apply_discount(price, pct):
if not 0 <= pct <= 100:
raise ValueError("pct out of range")
return round(price * (100 - pct) / 100, 2)
class DiscountTests(unittest.TestCase):
def setUp(self):
self.cart = {"pen": 10.0, "bag": 250.0}
def test_discount(self):
for price, pct, expected in [(100, 10, 90.0), (99.99, 0, 99.99), (50, 100, 0.0)]:
with self.subTest(price=price, pct=pct):
self.assertEqual(apply_discount(price, pct), expected)
def test_rejects_bad_pct(self):
with self.assertRaisesRegex(ValueError, "out of range"):
apply_discount(10, 120)
def test_cart_total(self):
self.assertEqual(sum(apply_discount(p, 10) for p in self.cart.values()), 234.0)
stream = io.StringIO()
result = unittest.TextTestRunner(stream=stream, verbosity=0).run(unittest.defaultTestLoader.loadTestsFromTestCase(DiscountTests))
print("ran", result.testsRun, "tests, ok =", result.wasSuccessful())Output:
text
ran 3 tests, ok = TrueThe unittest version runs in the standard library: subTest reports each input separately, assertRaisesRegex checks the exception type and message, and setUp plays the role of a fixture. The run prints only the result line, without timing, so the output is stable.
The same tests in pytest style (pytest is not installed on this box, so this block was not run here):
python
@pytest.fixture
def cart():
return {"pen": 10.0, "bag": 250.0}
@pytest.mark.parametrize("price,pct,expected", [(100, 10, 90.0), (99.99, 0, 99.99), (50, 100, 0.0)])
def test_discount(price, pct, expected):
assert apply_discount(price, pct) == expected
def test_rejects_bad_pct():
with pytest.raises(ValueError, match="out of range"):
apply_discount(10, 120)Keep tests independent of order and of the network, use temporary directories for files, and mock only at boundaries such as HTTP clients.
python
import io, unittest
def apply_discount(price, pct):
if not 0 <= pct <= 100:
raise ValueError("pct out of range")
return round(price * (100 - pct) / 100, 2)
class DiscountTests(unittest.TestCase):
def setUp(self):
self.cart = {"pen": 10.0, "bag": 250.0}
def test_discount(self):
for price, pct, expected in [(100, 10, 90.0), (99.99, 0, 99.99), (50, 100, 0.0)]:
with self.subTest(price=price, pct=pct):
self.assertEqual(apply_discount(price, pct), expected)
def test_rejects_bad_pct(self):
with self.assertRaisesRegex(ValueError, "out of range"):
apply_discount(10, 120)
def test_cart_total(self):
self.assertEqual(sum(apply_discount(p, 10) for p in self.cart.values()), 234.0)
stream = io.StringIO()
result = unittest.TextTestRunner(stream=stream, verbosity=0).run(unittest.defaultTestLoader.loadTestsFromTestCase(DiscountTests))
print("ran", result.testsRun, "tests, ok =", result.wasSuccessful())Run on Python 3.13.5, twice in fresh interpreters, with identical output.
Key points
- unittest is built in; pytest is the common runner
- Parametrise with subTest or parametrize
- Check exception type and message
- Fixtures or setUp provide shared setup
- Fast, isolated, deterministic tests
Common mistakes
- Tests that depend on each other's order
- Mocking the code under test
Follow-ups
- How do pytest fixture scopes work?
- When would you use monkeypatch or unittest.mock.patch?
Tips
- Show parametrised cases