上一小節我們學習了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文件名是固定的,不能修改
- contest.py文件不需要導入,pytest運行的時候會自動識別該文件
- conftest.py文件不能被其他文件導入
- 所有同目錄測試文件運行前都會執行conftest.py文件
- conftest.py與運行的用例要在同一個pakage下,并且有__init__.py文件
- conftest.py作用于文件同級目錄和子目錄下的所有測試用例,當有多個conftest.py文件的時候,子目錄的conftest.py文件優先級較高
- 如果希望fixture(夾具)共享給所有測試,則可以把conftest.py文件放在測試框架的根目錄下。
- 定義夾具@pytest.fixture的作用域參數scope:session,module,class,function
- 可以跨.py文件調用,有多個.py文件調用時,可讓conftest.py只調用了一次fixture,或調用多次fixture
conftest.py的使用
夾具(fixture)示例
conftest.py
import pytest@pytest.fixture()
def conftest_fixture():print("fixture前置")yieldprint("fixture后置")
test_demo.py
def test_case(conftest_fixture):print("測試用例")
運行結果
============================= test session starts =============================
collecting ... collected 1 itemtest_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")) # 表頭添加Descriptioncells.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 = yieldreport = outcome.get_result()report.description = str(item.function.__doc__)report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
test_demo.py
import pytestdef fun(x):return x + 1def test_answer_1():"""測試斷言一"""assert fun(3) == 4def 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.py文件,文件中的Fixture的作用范圍是所有測試模塊。
- 比如在某個單獨的測試文件夾里創建conftest.py文件,文件中Fixture的作用范圍,就僅局限于該測試文件夾里的測試模塊。
- 該測試文件夾外的測試模塊,或者該測試文件夾外的測試文件夾,是無法調用到這個conftest.py文件中的Fixture。
- 如果測試框架的根目錄和子包中都有conftest.py文件,并且這兩個conftest.py文件中都有一個同名的Fixture,實際生效的是測試框架中子包目錄下的conftest.py文件中配置的Fixture。
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 itemstest_1_baidu.py::test_01
打開首頁
打開百度頁面_session
PASSED [ 50%]測試用例test_01test_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.pyimport pytestdef test_03(start, open_blog):print("測試用例test_03")assert 1def test_04(start, open_blog):print("測試用例test_04")assert 1def test_05(start, open_baidu):'''跨模塊調用baidu模塊下的conftest'''print("測試用例test_05,跨模塊調用baidu")assert 1if __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 itemstest_2_blog.py::test_03
打開首頁
打開blog頁面_function
PASSED [ 33%]測試用例test_03test_2_blog.py::test_04 打開blog頁面_function
PASSED [ 66%]測試用例test_04test_2_blog.py::test_05 ERROR [100%]
test setup failed
file D:\PycharmProjects\Source_Code\pytest_demo\blog\test_2_blog.py, line 14def 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 ==========================
最后感謝每一個認真閱讀我文章的人,禮尚往來總是要有的,雖然不是什么很值錢的東西,如果你用得到的話可以直接拿走,希望可以幫助到大家!