Pytest
Pytest minimum example
# test_basic.py
def test_something():
assert True
Parametrize example
Creating multiple tests with a single function
import pytest
def is_even(input):
if input % 2 == 0:
return True
return False
@pytest.mark.parametrize("input,expected", [
(2, True),
(3, False),
(11, False),
])
def test_is_even(input, expected):
assert is_even(input) == expected
Assert raises an error
import pytest
def do_something(input):
if input == 0:
raise ValueError('A very specific bad thing happened.')
return True
def test_do_something():
with pytest.raises(ValueError):
do_something(0)
Basic example of fixtures
import pytest
@pytest.fixture
def user():
return {
'name': 'John Snow',
'email': '[email protected]'
}
def test_do_something(user):
assert user['name'] == 'John Snow'