5.pytest中fixture的使用(params参数)

上一篇文章写到fixture中的scope参数的使用,本篇文章主要写params参数的使用;params是fixture函数中的一个可选参数列表,它将导致多个参数调用fixture函数和所有测试使用它。

conftest.py 文件

@pytest.fixture(scope='session',params=['chrome','firefox'])
def fix_test(request):
    print('----- 创建浏览器驱动器对象 -----')
    yield request.param
    print('销毁浏览器驱动器对象')
测试用例文件

class Test_web():

    def test1(self,fix_test):
        print('%s-----打开京东'%fix_test)

    def test2(self,fix_test):
        print('%s-----打开唯品会' % fix_test)

可以看到结果是根据params参数值运行了两次测试用例;可以通过fixture函数的scope参数控制作用域;

原文地址:https://www.cnblogs.com/XhyTechnologyShare/p/12263447.html