上一小节我们学习了fixture的作用域,本小节我们学习一下pytest conftest.py文件的使用方法。
conftest.py文件的作用
conftest.py文件是pytest框架中的一个特殊文件,用于定义共享的设置、夹具(fixture)和钩子函数(hook)。
在pytest中,conftest.py文件可以用于在整个测试项目中共享夹具、配置和钩子函数。通过在conftest.py文件中定义夹具,你可以提供测试所需的初始化数据和对象,并使其在测试文件中可用。这样可以避免在每个测试文件中重复定义夹具,提高代码的复用性和可维护性。
此外,conftest.py文件也可以定义钩子函数,用于在测试执行的不同阶段插入自定义的行为。通过定义钩子函数,你可以在测试开始前、测试结束后或其他特定的测试事件发生时执行特定的代码逻辑。这样可以扩展和定制pytest的行为,实现特定的测试需求和额外的操作。
当pytest运行时,它会自动搜索项目中的conftest.py文件,并根据其中的定义来加载夹具和钩子函数。conftest.py文件可以位于项目的根目录下,也可以位于子目录中,它们会在对应的作用域内生效。
conftest.py文件的特点
conftest.py的使用
夹具(fixture)示例
conftest.py
- import pytest
-
-
- @pytest.fixture()
- def conftest_fixture():
- print("fixture前置")
- yield
- print("fixture后置")
test_demo.py
- def test_case(conftest_fixture):
- print("测试用例")
运行结果
- ============================= test session starts =============================
- collecting ... collected 1 item
-
- test_demo.py::test_case fixture前置
- PASSED [100%]测试用例
- fixture后置
-
-
- ============================== 1 passed in 0.02s ==============================
钩子函数(hook)示例
比如在pytest教程-9-pytest-html生成html报告这一小节中,使用钩子函数来定制html报告就是一个比较好的例子。
conftest.py
- # conftest.py
- import pytest
- from py._xmlgen import html
- from datetime import datetime
-
-
- # 1、修改报告标题
- def pytest_html_report_title(report):
- report.title = "我的测试报告标题"
-
-
- # 2、运行测试前修改环境信息
- @pytest.hookimpl(optionalhook=True)
- def pytest_metadata(metadata: dict):
- metadata['项目名称'] = '我的项目'
- metadata['接口地址'] = "https://www.example.com"
-
-
- # 3、修改摘要信息
- def pytest_html_results_summary(prefix, summary, postfix):
- prefix.extend([html.p("所属部门: 测试保障部")])
- prefix.extend([html.p("测试人员: 张三")])
-
-
- # 4、测试结果表格
- @pytest.mark.optionalhook
- def pytest_html_results_table_header(cells):
- cells.insert(1, html.th("Description")) # 表头添加Description
- cells.insert(2, html.th("Time", class_="sortable time", col="time"))
- cells.pop(-1) # 删除link
-
-
- @pytest.mark.optionalhook
- def pytest_html_results_table_row(report, cells):
- cells.insert(1, html.td(report.description)) # 表头对应的内容
- cells.insert(2, html.td(datetime.now(), class_="col-time"))
- cells.pop(-1) # 删除link
-
-
- @pytest.mark.hookwrapper
- def pytest_runtest_makereport(item, call): # Description取值为用例说明__doc__
- outcome = yield
- report = outcome.get_result()
- report.description = str(item.function.__doc__)
- report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
test_demo.py
- import pytest
-
-
- def fun(x):
- return x + 1
-
-
- def test_answer_1():
- """测试断言一"""
- assert fun(3) == 4
-
-
- def test_answer_2():
- """测试断言二"""
- assert fun(5) == 7
-
-
- @pytest.mark.parametrize("test_input,expected", [
- ("3+5", 8),
- ("2+4", 6),
- pytest.param("6 * 9", 42, marks=pytest.mark.xfail),
- pytest.param("6 * 6", 42, marks=pytest.mark.skip)
- ])
- def test_mark(test_input, expected):
- """用例集合"""
- assert eval(test_input) == expected
修改完成,重新执行脚本,查看最终效果。

conftest.py文件作用域
conftest层级关系
在pytest_demo项目工程下建两个子项目baidu、blog,并且每个目录下都放一个conftest.py和init.py(python的每个package必须要有init.py)
- pytest_demo是工程名称
-
- ├─baidu
- │ │ conftest.py
- │ │ test_1_baidu.py
- │ │ __init__.py
- │
- │
- ├─blog
- │ │ conftest.py
- │ │ test_2_blog.py
- │ │ __init__.py
- │
- │ conftest.py
- │ __init__.py

案例分析
pytest_demo工程下conftest.py文件代码案例
- # pytest_demo/conftest.py
- import pytest
-
- @pytest.fixture(scope="session")
- def start():
- print("\n打开首页")
baidu目录下conftest.py和test_1_baidu.py
运行test_1_baidu.py结果可以看出,start和open_baidu是session级别的,只运行一次
- ============================= test session starts =============================
- collecting ... collected 2 items
-
- test_1_baidu.py::test_01
- 打开首页
- 打开百度页面_session
- PASSED [ 50%]测试用例test_01
-
- test_1_baidu.py::test_02 PASSED [100%]测试用例test_02
-
-
- ============================== 2 passed in 0.02s ==============================
blog目录下conftest.py和test_2_blog.py代码
- # pytest_demo/blog/conftest.py
- import pytest
-
- @pytest.fixture(scope="function")
- def open_blog():
- print("打开blog页面_function")
-
-
- # web_conf_py/blog/test_2_blog.py
-
- import pytest
-
- def test_03(start, open_blog):
- print("测试用例test_03")
- assert 1
-
- def test_04(start, open_blog):
- print("测试用例test_04")
- assert 1
-
- def test_05(start, open_baidu):
- '''跨模块调用baidu模块下的conftest'''
- print("测试用例test_05,跨模块调用baidu")
- assert 1
-
- if __name__ == "__main__":
- pytest.main(["-s", "test_2_blog.py"])
运行结果可以看出,start起到全局作用,blog目录下的open_blog是function级别,每个用例调用一次。
test_05(start, open_baidu)用例不能跨模块调用baidu模块下的open_baidu,所以test_05用例会运行失败
- ============================= test session starts =============================
- collecting ... collected 3 items
-
- test_2_blog.py::test_03
- 打开首页
- 打开blog页面_function
- PASSED [ 33%]测试用例test_03
-
- test_2_blog.py::test_04 打开blog页面_function
- PASSED [ 66%]测试用例test_04
-
- test_2_blog.py::test_05 ERROR [100%]
- test setup failed
- file D:\PycharmProjects\Source_Code\pytest_demo\blog\test_2_blog.py, line 14
- def test_05(start, open_baidu):
- E fixture 'open_baidu' not found
- > available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, extra, extras, include_metadata_in_junit_xml, metadata, monkeypatch, open_blog, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, start, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
- > use 'pytest --fixtures [testpath]' for help on them.
-
- D:\PycharmProjects\Source_Code\pytest_demo\blog\test_2_blog.py:14
-
-
- ========================= 2 passed, 1 error in 0.03s ==========================
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走,希望可以帮助到大家!
