一、fixture里面有个参数scope,通过scope可以控制fixture的作用范围,根据作用范围大小划分:session>module>class>function,具体作用范围如下:
1.function 函数或者方法级别都会被调用
2.class 类级别调用一次
3.module 模块级别调用一次
4.session 是个多文件调用一次(可以跨 .py 文件调用,每个.py文件就是 module)
例如整个模块有多条测试用例,需要在全部用例执行之前打开浏览器,全部执行完之后去关闭浏览器,打开和关闭操作只执行一次,如果每次都要重新执行打开操作,会非常占用系统资源,这种场景除了 setup——module,teardown_module 可以实现,还可以通过设置模块级别的 fixture装饰器【@pytst.fixture(scope="module")】来实现
scope='module'
fixture 参数 scope=‘module’ ,module作用是整个模块都会生效
#!/usr/bin/env python # _*_coding: utf-8 _*_ import pytest @pytest.fixture(scope='module') def open(): print("打开浏览器") yield print("执行teardown !") print("最后关闭浏览器") @pytest.mark.usefixtures("open") def test_search1(): print("test_search1") raise NameError pass def test_search2(): print("test_search2") pass def test_search3(): print("test_search3") pass
代码解析:@pytest.fixture()如果不写参数,参数默认scope=‘function’。当scope='module'时,在当前 .py脚本里面所有的用例开始前只执行一次。scope巧妙与yield组合使用,相当于setup和teardown方法。还可以使用@pytest.mark.usefixtures装饰器,传入前置函数名作为参数
运行结果如下:
Testing started at 12:03 ... C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture_scope.py Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture_scope.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123 ============================= test session starts ============================= platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123 plugins: html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items test_fixture_scope.py 打开浏览器 Ftest_search1 test_fixture_scope.py:14 (test_search1) @pytest.mark.usefixtures("open") def test_search1(): print("test_search1") > raise NameError E NameError test_fixture_scope.py:18: NameError .test_search2 .test_search3 执行teardown ! 最后关闭浏览器 [100%] ================================== FAILURES =================================== ________________________________ test_search1 _________________________________ @pytest.mark.usefixtures("open") def test_search1(): print("test_search1") > raise NameError E NameError test_fixture_scope.py:18: NameError ---------------------------- Captured stdout setup ---------------------------- 打开浏览器 ---------------------------- Captured stdout call ----------------------------- test_search1 =========================== short test summary info =========================== FAILED test_fixture_scope.py::test_search1 - NameError ========================= 1 failed, 2 passed in 0.19s ========================= Process finished with exit code 0 Assertion failed Assertion failed
从上面运行结果可以看出scope=‘module’ 和yield结合,相当于setup_module 和teardown_module方法,整个模块运行之前调用open()方法中的yield前面打印输出 的打开浏览器 ,整个运行之后调用了yield后面的打印语句执行teardown与关闭浏览器,yield来唤醒teardown的执行,如果用例出现异常,不影响yield后面teardown执行
可以使用 @pytest.mark.usefixtures装饰器来进行方法的传入