pytest提供了标记机制,运行使用marker对测试用例做标记。一个测试用例可以有多个marker,一个marker也可以用来标记多个测试用例
test_003.py
#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest
def test_01():
print(‘test_01‘)
@pytest.mark.aaa
def test_02():
print(‘test_02‘)
class Test_Class():
@pytest.mark.aaa
def test_03(self):
print(‘test_03‘)
执行
pytest -vm "aaa"
结果
================================================== test session starts ===================================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/_dde_data/python
collected 7 items / 5 deselected / 2 selected
test_003.py::test_02 PASSED [ 50%]
test_003.py::Test_Class::test_03 PASSED [100%]
==================================================== warnings summary ====================================================
test_003.py:9
/media/_dde_data/python/test_003.py:9: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
@pytest.mark.aaa
test_003.py:14
/media/_dde_data/python/test_003.py:14: PytestUnknownMarkWarning: Unknown pytest.mark.aaa - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
@pytest.mark.aaa
-- Docs: https://docs.pytest.org/en/latest/warnings.html
====================================== 2 passed, 5 deselected, 2 warnings in 0.02s =======================================
#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest
@pytest.mark.skip(reason="跳过test_one")
def test_one():
print(‘test_one‘)
def test_two():
if True:
pytest.skip(‘跳过test_two‘)
print(‘test_two‘)
class Test_Class():
def test_three(self):
print(‘test_three‘)
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/_dde_data/python
collected 3 items
test_001.py::test_one SKIPPED
test_001.py::test_two SKIPPED
test_001.py::Test_Class::test_three test_three
PASSED
================================ 1 passed, 2 skipped in 0.02s ================================
#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest
import sys
@pytest.mark.skipif(sys.platform != "linux2",reason="如果不是linux平台跳过")
def test_one():
print(‘test_one‘)
def test_two():
print(‘test_two‘)
class Test_Class():
def test_three(self):
print(‘test_three‘)
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/_dde_data/python
collected 3 items
test_001.py::test_one SKIPPED
test_001.py::test_two test_two
PASSED
test_001.py::Test_Class::test_three test_three
PASSED
================================ 2 passed, 1 skipped in 0.01s ================================
#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest
import sys
@pytest.mark.xfail(reason = "预期失败")
def test_one():
assert 1 == 2
@pytest.mark.xfail(reason = "预期失败")
def test_two():
assert 1 == 1
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/_dde_data/python
collected 2 items
test_001.py::test_one XFAIL
test_001.py::test_two XPASS
=============================== 1 xfailed, 1 xpassed in 0.03s ================================
原文:https://www.cnblogs.com/jingxindeyi/p/13348252.html