若是第三方插件知足不了本身的需求,那么就须要你本身定制化开发本身的插件了。html
正因如此,pytest的插件生态很是的丰富,通常的常规性需求基本上均可以找到现成的第三方插件。python
说到开发插件的话,仍是要引入一个新的名词hook functions ,对于钩子函数是要另起一篇介绍的,那也是真正开发插件的开始。session
一个插件里包含了一个或者多个钩子函数,编写钩子函数能够实现功能丰富的插件,因此pytest框架从配置、收集用例到运行用例,再到最后出报告,都是经过调用各类插件来实现的。框架
目前所说插件就是如下这3种了:函数
_pytest
这个目录下的模块(.\Python37\Lib\site-packages_pytest)conftest.py
,这个能够理解成框架的固定写法,把hook
或者fixture
写在这个文件里,就会自动去调用。内置插件、外部插件都仍是很好理解的,那conftest
文件是什么?
其实,它是一个python文件。
开个玩笑,其实conftest.py
一般能够用来作2个事情:测试
另外,能够根据conftest.py
的文件存放位置,来决定它的适用范围。插件
在以前的文章【pytest】(六) pytest中fixture的使用 中,咱们用到的fixture函数都是写在了测试case的文件中。
好比:fixture
函数logout_after_login_success()
是在测试case test_login_success
执行结束后执行,用来退出登陆。code
@pytest.fixture() def logout_after_login_success(browser): yield HP = HomePage(browser) HP.logout() @pytest.mark.critical def test_login_success(browser, logout_after_login_success, login_temp): '''合法登陆''' HP = HomePage(browser) assert HP.greetings().text == "Hi, test1"
那若是你发现你的一个fixture
函数,在不少case模块中都要用到,这时候你要怎么引用呢?orm
不要紧,咱们能够把这个fixture
函数放到conftest.py
中去。
好比说,我如今有一个项目结构是这样的:htm
-demo_project -test_case -test_module_01 /* test_case1.py /* test_case2.py -test_module_02 /* test_case3.py -utils
在test_module_01
模块下,有2个case用例文件,分别是 test_case1.py
和test_case2.py
。
在test_module_02
模块下,有一个case用例文件test_case3.py
。
如今若是想要test_module_01
模块下的2个测试用例都用上同一个fixture
函数,那么就能够在test_module_01
模块下,新建一个与2个case文件同级的conftest.py
,文件结构就变成了这样:
-demo_project -test_case -test_module_01 /* test_case1.py /* test_case2.py /* conftest.py -----> 在这 -test_module_02 /* test_case3.py -utils
test_case1.py
、test_case2.py
和conftest.py
代码分别为:
# test_case1.py import pytest def test_01(demo_fixture): print("执行了test_01") if __name__ == '__main__': pytest.main(["-s", "test_case1.py"])
# test_case2.py import pytest def test_02(demo_fixture): print("执行了test_02") if __name__ == '__main__': pytest.main(["-s", "test_case2.py"])
# /test_module_01/conftest.py import pytest @pytest.fixture def demo_fixture(): print("这是fixture函数的输出")
分别执行test_case1.py
和test_case2.py
, 都输出了demo_fixture()函数的内容。
============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 rootdir: D:\练习\demo_project\test_case\test_module_01 collected 1 item test_case1.py 这是fixture函数的输出 执行了test_01 . ============================== 1 passed in 0.01s ============================== [Finished in 0.4s]
============================= test session starts ============================= platform win32 -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1 rootdir: D:\练习\demo_project\test_case\test_module_01 collected 1 item test_case2.py 这是fixture函数的输出 执行了test_02 . ============================== 1 passed in 0.02s ============================== [Finished in 0.4s]
再继续,若是test_module_02
模块下的也想用,怎么办?
那就把conftest.py
再往外提一层,与 test_module_01
、test_module_02
保持同级便可。
-demo_project -test_case -test_module_01 /* test_case1.py /* test_case2.py -test_module_02 /* test_case3.py /* conftest.py -utils
你们能够试一下,这里就不继续代码演示了。
总结来讲,conftest.py
文件能够做用于同级
以及 如下
的模块。
可是,当如下层级中也存在了另外一个conftest.py
,那么如下层级将由另外一个conftest.py
文件接管。
放个图示辅助说明一下:
2张图能说的事,水了这么多,哈哈。