Tahák · Python · Profesionální vývoj
Testování: unittest a pytest
Napíšeš testy, které opravdu chytají chyby. Hranice, výjimky, příprava dat, unittest, pytest a proč 100% pokrytí nic neslibuje.
pythonimport unittest
class TestX(unittest.TestCase):
def setUp(self):
self.data = [] # před každým testem
self.addCleanup(self.data.clear) # úklid i po selhání
def test_y(self):
self.assertEqual(1 + 1, 2)
with self.assertRaises(ValueError):
int("x")
for a in [1, 2]:
with self.subTest(a=a):
self.assertGreater(a, 0)
# pytest
def test_z(tmp_path):
assert 1 + 1 == 2
with pytest.raises(ValueError, match="invalid"):
int("x")
bashpython -m unittest discover -s tests -v
pytest -q -x --lf
pytest --cov=balicek --cov-branch --cov-report=term-missing