pytest#
Fixtures#
import pytest
@pytest.fixture
def my_value():
return 5
def test_my_fruit_in_basket(my_value):
result = my_value + 5
assert result == 10
Temporary Directories#
fixture
tmp_path
: provides apathlib.Path
objectfixture
tmpdir
: provides a legacypy.path.local
object
Parametrize#
import pytest
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
def test_eval(test_input, expected):
assert eval(test_input) == expected
Assert expected Exception#
import pytest
def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0
Skip Tests#
import pytest
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
...
Flaky Tests#
rerun flaky tests
import pytest
# reruns_delay is in seconds and optional
@pytest.mark.flaky(reruns=5, reruns_delay=2)
def test_example():
assert random.choice([True, False])