pytest-标记

标记

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 =======================================

内置标记

  1. 跳过(skip)
    skip和skipif运行跳过不希望执行的用例
#!/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 ================================
  1. 有条件跳过(skipif)
#!/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 ================================
  1. 标记预期失败的用例(xfail)
    使用skip和skipif标记的用例会直接跳过,而不会执行。使用xfail标记的用例则会执行,但我们预期失败,如果执行失败状态为XFAIL,如果执行成功则为XPASS
#!/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 ================================
posted @ 2020-07-20 22:39  静心&得意  阅读(178)  评论(0编辑  收藏  举报