其他分享
首页 > 其他分享> > pytest 框架的高级使用方法

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

标签:__,name,框架,py,高级,TestDemo,pytest,test
来源: https://www.cnblogs.com/czzz/p/15720828.html