拓冰建站拓冰建站
首页 / 资讯中心 / 正文

Pytest fixture机制

Pytest fixture机制通过fixture可以把公共方法参数化通过pytest.fixture( )装饰的函数可以作为参数传入到测试用例中当执行这个测试用例时会优先执行fixture装饰的函数方便代码复用减少冗余可理解成公共方法的封装场景在执行把商品加入购物车的测试用例时先要完成用户登录接口自动化测试执行购买时需要传入用户token# test_mod8.py import pytest pytest.fixture() def login(): print(账号进行登录) def test_add_shopCar(login): print(把商品加入购物车)执行结果test_mod8.py::test_add_shopCar 账号进行登录 把商品加入购物车 PASSED 1 passed in 0.03s 测试用例 test_add_shopCar中传入参数是login因此执行test_add_shopCar测试用例时会首先执行login函数import pytest pytest.fixture() def add_func(): return 32 def test_one(add_func): assert add_func 4 def test_two(add_func): assert add_func 3test_mod8.py::test_one PASSED test_mod8.py::test_two FAILED FAILURES 当test_one和test_two测试用例接收 add_func 函数时 fixture的作用是实例化函数 然后返回add_ func 的值在多个测试模块.py中共用一个fixture可以把fixture移动到conftest.py中pytest会自动导入不需要手动导入# conftest.py import pytest pytest.fixture() def login(): print(账号进行登录) pytest.fixture() def add_func(): return 32# test_mod8.py import pytest def test_add_shopCar(login): print(把商品加入购物车) def test_one(add_func): assert add_func 4 def test_two(add_func): assert add_func 3执行结果test_mod8.py::test_add_shopCar 账号进行登录 把商品加入购物车 PASSED test_mod8.py::test_one PASSED test_mod8.py::test_two FAILED FAILURES 上述代码test_mod8.py 模块中test_add_shopCartest_one test_two测试函数中入参分别为conftest.py模块中fixture装饰的login()add_func()从而实现了fixture装饰下的函数前置功能可供多个测试模块调用fixture 自动导入的顺序 测试类 测试模块 conftest.py# conftest.py import pytest pytest.fixture() def re_str(): return 这是conftest中的strpytest.fixture() def re_str(): return 这是模块中的str class TestMod8: pytest.fixture() def re_str(self): return 这是class中的str def test_three(self,re_str): print(re_str)上述代码分别在测试类测试模块conftest.py中定义了re_str并通过pytest.fixture()装饰直接通过pytest运行test_three结果test_mod8.py::TestMod8::test_three 这是class中的str PASSED 1 passed in 0.43s test_three中的re_str调用的是类中的re_str如果注释掉类中的re_str再次运行pytest.fixture() def re_str(): return 这是模块中的str class TestMod8: # pytest.fixture() # def re_str(self): # return 这是class中的str def test_three(self,re_str): print(re_str)结果test_mod8.py::TestMod8::test_three 这是模块中的str PASSED 1 passed in 0.20s 当conftest.py中也存在re_str时测试用例首先执行模块中的re_str只有当类中的fixture与模块中的fixture都不存在时才会引用conftest.py中的fixturefixture中的scope参数fixture为session级别是可以跨.py模块调用的也就是当我们有多个.py文件的用例的时候如果多个用例只需调用一次fixture那就可以设置为scopesession并且写到conftest.py文件里。package 会作用于包内的每一个测试用例, 且只被调用一次function 每个测试用例都会调用一次class 如果一个class 中有多个测试用例 在类中只调用一次fixture中的autouse参数True, False是否自动调用即测试用例中不需要传入fixture修饰的函数名也可完成调用# conftest.py import pytest pytest.fixture(scopemodule,autouseTrue) def module_fixture(): print(这是conftest.py中的module_fixture)# test_mod8 def test_five(): print(正在执行测试用例-------test_five) class TestMod8: def test_three(self): print(正在执行测试用例-------test_three) def test_four(self): print(正在执行测试用例-------test_four)执行结果test_mod8.py::test_five 这是conftest.py中的module_fixture 正在执行测试用例-------test_five PASSED test_mod8.py::TestMod8::test_three 正在执行测试用例-------test_three PASSED test_mod8.py::TestMod8::test_four 正在执行测试用例-------test_four PASSED 3 passed in 0.56s 任务针对上述代码pytest.fixture(scopemodule,autouseTrue) 把scope分别更改为functionclasspackage再运行通过 fixture 参数化# test_mod8.py import pytest l [1,2,3,4,5] pytest.fixture(paramsl) def read_params(request): s request.param return s def test_five(read_params): x read_params print(f正在执行测试用例-------test_five,传入参数{x})命令行pytest -vs test_mod8.py::test_five结果 test_mod8.py::test_five[1] 正在执行测试用例-------test_five,传入参数1 PASSED test_mod8.py::test_five[2] 正在执行测试用例-------test_five,传入参数2 PASSED test_mod8.py::test_five[3] 正在执行测试用例-------test_five,传入参数3 PASSED test_mod8.py::test_five[4] 正在执行测试用例-------test_five,传入参数4 PASSED test_mod8.py::test_five[5] 正在执行测试用例-------test_five,传入参数5 PASSED 5 passed in 0.20s fixture 中传入的params参数为一个listfixture装饰的函数传入request参数通过request.param来遍历params中的元素并返回注params是一个 list 类型通过fixture yield 实现setup和teardown# conftest.py import pytest pytest.fixture(scopefunction,autouseTrue) #定义这个fixture作用域为每个测试用例都会调用 def function_fixture(): print(用例执行之前执行相当于setup) yield # yield之前的代码在执行测试用例前执行 # yield之后的代码在执行测试用例后执行 print(用例执行之后执行相当于teardown)import pytest class TestMod8: def test_three(self): print(正在执行测试用例-------test_three) def test_four(self): print(正在执行测试用例-------test_four)执行结果test_mod8.py::TestMod8::test_three 用例执行之前执行相当于setup 正在执行测试用例-------test_three PASSED用例执行之后执行相当于teardown test_mod8.py::TestMod8::test_four 用例执行之前执行相当于setup 正在执行测试用例-------test_four PASSED用例执行之后执行相当于teardown 2 passed in 0.07s
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门