pytest 框架的高级使用方法

跳过测试方法

语法@pytest.mark.skipif(条件, reason="原因")


"""演示 pytest 跳过方法的使用"""
import pytest

version = 25

class TestDemo:

    def test_demo1(self):
        print("this is demo1")

    def test_demo2(self):
        print("this is a demo2")

    @pytest.mark.skipif(version >= 25, reason="xxxxxxxxx")
    def test_demo2(self):
        print("this is a demo2")


if __name__ == '__main__':
    pytest.main(['-s', "06_pyest_skip.py"])

执行后结果如下:

============================= test session starts =============================
collecting ... collected 2 items

06_pyest_skip.py::TestDemo::test_demo1 this is demo1
PASSED
06_pyest_skip.py::TestDemo::test_demo2 SKIPPED (xxxxxxxxx)
Skipped: xxxxxxxxx


======================== 1 passed, 1 skipped in 0.01s =========================

参数化:单个参数

语法:@pytest.mark.parameterize(参数变量,['数值1', '数值2',...])
demo:

"""
pytest 参数化演示:单个值
"""
import pytest


class TestDemo(object):

    @pytest.mark.parametrize('name', ['张飞', '关羽', '刘备'])
    def test_demo01(self, name):
        print("my name is %s " % name)


if __name__ == '__main__':
    pytest.main(['-s', '07_pytest_parameterized.py'])

结果:

============================= test session starts =============================
collecting ... collected 3 items

07_pytest_parameterized.py::TestDemo::test_demo01[\u5f20\u98de] my name is 张飞 
PASSED
07_pytest_parameterized.py::TestDemo::test_demo01[\u5173\u7fbd] my name is 关羽 
PASSED
07_pytest_parameterized.py::TestDemo::test_demo01[\u5218\u5907] my name is 刘备 
PASSED

============================== 3 passed in 0.01s ==============================
上一篇:计算机网络面试题(一)


下一篇:面试官问我:从地址栏输入URL到显示页面都发生了什么?