Introduction aux tests en Python
Alexander Levin
Data Scientist
Cas d'usage 2 : ce test devrait échouer
Marqueur de test : une étiquette (marqueur) d'un test dans la bibliothèque pytest
@pytest.mark :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 : quand vous voulez ignorer un test en tout temps@pytest.mark.skipif : pour ignorer un test si une condition donnée est True@pytest.mark.skip : lorsque vous voulez ignorer un test indéfiniment.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 : pour ignorer un test si la condition donnée est 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 : quand vous vous attendez à l'échec d'un testimport 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)

Marqueur de test :
pytest@pytest.mark.name_of_the_marker@pytest.mark.xfail@pytest.mark.skip@pytest.mark.skipifIntroduction aux tests en Python