Introduction to Testing in Python
Alexander Levin
Data Scientist
Use case 2: this test is expected to fail
Test marker - a tag (a marker) of a test in the pytest library
@pytest.mark decorator:import pytest
def get_length(string):
return len(string)
# The test marker syntax
@pytest.mark.skip
def test_get_len():
assert get_length('123') == 3
@pytest.mark.skip - when you want a test to be skipped in any case@pytest.mark.skipif - if you want a test to be skipped if a given condition is True@pytest.mark.skip - when you want a test to be skipped indefinitely.import pytest
def get_length(string):
return len(string)
# The skip marker example
@pytest.mark.skip
def test_get_len():
assert get_length('123') == 3

@pytest.mark.skipif - when you want a test to be skipped if the given condition is True.import pytest
def get_length(string):
return len(string)
# The skipif marker example
@pytest.mark.skipif('2 * 2 == 5')
def test_get_len():
assert get_length('abc') == 3

@pytest.mark.xfail - when you expect a test to be failedimport pytest
def gen_sequence(n):
return list(range(1, n+1))
# The xfail marker example
@pytest.mark.xfail
def test_gen_seq():
assert gen_sequence(-1)

Test marker:
pytest library@pytest.mark.name_of_the_marker@pytest.mark.xfail@pytest.mark.skip@pytest.mark.skipifIntroduction to Testing in Python