0 dní v řadě · 0 bodů

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.

Otevřít celou lekciVšechny taháky
python
import 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")
bash
python -m unittest discover -s tests -v
pytest -q -x --lf
pytest --cov=balicek --cov-branch --cov-report=term-missing