首页 > 其他 > 详细

pytest-标记

时间:2020-07-21 00:29:05      阅读:81      评论:0      收藏:0      [点我收藏+]

标记

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

pytest-标记

原文:https://www.cnblogs.com/jingxindeyi/p/13348252.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!