Marks

Used to create a set of tests

@pytest.mark.smoke
def test_add():
	assert add(1, 2) == 3

# pytest -v -m 'smoke' test_calculator.py

Skipping the tests

@pytest.mark.skip(reason='misunderstood the API')
def test_smth(): ...

# To see the reason => pytest -rs test_unique_id_3.py
@pytest.mark.skipif(tasks.__version__ < '0.2.0', reason='not supported until version 0.2.0')
def test_unique_id_1(): ...

Marking Tests as Expecting to Fail

@pytest.mark.xfail()
def test_unique_id_is_a_duck():
    """Demonstrate xfail."""
		uid = tasks.unique_id() 
		assert uid == 'a duck'
image

Parametrized Testing

  • Parametrized testing is a way to send multiple sets of data through the same test and have pytest report if any of the sets failed.
  • @pytest.mark.parametrize(argnames: str, argvalues: list)
  • parametrize() with classes. When you do that, the same data sets will be sent to all test methods in the class.
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

# test_add_variety.py::test_eval[test_input-expected] PASSED
image
  • id with @pytest.mark.parametrize() decorator. You do this with pytest.param(<value>, id="something") syntax. Or ids=lambda x: f"{x}"
SuperMade with Super