what is it?

A way to add meta data to tests

skip a test

import pytest
 
@pytest.mark.skip
def test_example():
	assert 1 == 1

mark a test that is a known failure

import pytest
 
@pytest.mark.xfail
def test_example():
	assert 1 == 2

custom markers

  • edit pytest.ini

    [pytest]
    DJANGO_SETTINGS_MODULE = fica_face.settings
    # define where tests are collected from
    python_files = test_*.py *_tests.py
    markers = 
    	slow: slow running tests
  • mark test as slow

    import pytest
     
    @pytest.mark.slow
    def test_example():
    	assert 1 == 2
  • only run slow tests: pytest -m "slow"

  • skip slow tests: pytest -m "not slow"

database access

  • @pytest.mark.django_db

repeat test with different parameters

@pytest.mark.parametrize("result, msg", [
	(False, "dummy msg"),
	(True, "dummy msg")
])
def test_example(result, msg):
	resp = method_to_test()
 
	assert resp == (result, msg)

skipif

skip test if python version is below 3.3:

@pytest.mark.skipif(sys.version_info < (3, 3), reason='does not support XXX')
def test_abc():
    pass