本文旨在解決 pytest 從 4.x 升級(jí)到 5.x+ 后,`pytest.config` 被移除導(dǎo)致無(wú)法通過(guò)命令行標(biāo)志條件運(yùn)行或跳過(guò)特定測(cè)試的問(wèn)題。我們將介紹如何利用 pytest 5.x+ 及更高版本中的自定義標(biāo)記(`pytest.mark`)與 `-m` 命令行選項(xiàng),優(yōu)雅地實(shí)現(xiàn)對(duì)帶有特定裝飾器的測(cè)試進(jìn)行靈活的選擇性執(zhí)行,從而避免大規(guī)模代碼修改。此方法不僅保持了代碼的簡(jiǎn)潔性,也提升了測(cè)試管理的效率和靈活性。
在 Pytest 4.x 版本中,開(kāi)發(fā)者通常會(huì)利用 pytest.config.getoption 方法結(jié)合自定義裝飾器來(lái)根據(jù)命令行參數(shù)動(dòng)態(tài)控制特定測(cè)試的運(yùn)行或跳過(guò)。例如,以下代碼展示了如何定義一個(gè) integration 裝飾器,使其在 --integration 命令行標(biāo)志不存在時(shí)自動(dòng)跳過(guò)集成測(cè)試:
# common.py (Pytest 4.x 示例) import pytest integration = pytest.mark.skipif( not pytest.config.getoption('--integration', False), reason="Integration tests require --integration flag" )
然后,在測(cè)試文件中,這些裝飾器可以被方便地應(yīng)用于相關(guān)測(cè)試函數(shù):
# test_something.py (Pytest 4.x 示例) from .common import integration @integration def test_mytest(): assert 1 == 1 @integration def test_other_mytest(): assert 2 == 2
然而,從 Pytest 5.x+ 版本開(kāi)始,pytest.config 屬性已被移除,導(dǎo)致上述代碼在運(yùn)行時(shí)會(huì)拋出 AttributeError: module 'pytest' has no attribute 'config' 錯(cuò)誤。對(duì)于那些擁有大量使用此類(lèi)裝飾器的現(xiàn)有測(cè)試的項(xiàng)目來(lái)說(shuō),如何在不進(jìn)行大規(guī)模代碼重構(gòu)的前提下,在 Pytest 5.x+ 中實(shí)現(xiàn)相同的靈活性,成為了一個(gè)亟待解決的問(wèn)題。
Pytest 5.x+ 提供了一種更簡(jiǎn)潔、更標(biāo)準(zhǔn)化的方式來(lái)解決這個(gè)問(wèn)題,即通過(guò)自定義標(biāo)記(Custom Markers)與內(nèi)置的 -m 命令行選項(xiàng)。這種方法不僅能夠?qū)崿F(xiàn)與舊版 pytest.config 相同的功能,而且更加符合 Pytest 的設(shè)計(jì)哲學(xué),并且對(duì)現(xiàn)有測(cè)試代碼的改動(dòng)極小。
首先,我們需要重新定義 integration 裝飾器。在 Pytest 5.x+ 中,我們不再需要 pytest.config 來(lái)檢查命令行選項(xiàng),而是直接使用 pytest.mark 來(lái)創(chuàng)建標(biāo)記。
# common.py (Pytest 5.x+ 解決方案) import pytest # 直接定義一個(gè)名為 'integration' 的標(biāo)記 integration = pytest.mark.integration
然后,在測(cè)試文件中,繼續(xù)使用這個(gè)新的 integration 裝飾器來(lái)標(biāo)記需要特殊處理的測(cè)試:
# test_skip.py (Pytest 5.x+ 解決方案) from .common import integration @integration def test1(): assert True def test2(): # 未被標(biāo)記的測(cè)試 assert True
為了讓 Pytest 識(shí)別我們自定義的 integration 標(biāo)記,并避免產(chǎn)生 PytestUnknownMarkWarning 警告,我們需要在項(xiàng)目根目錄下的 pytest.ini 配置文件中注冊(cè)它。
# pytest.ini [pytest] markers = integration: marks tests as integration tests (deselect with '-m "not integration"')
在 markers 部分,每行定義一個(gè)標(biāo)記,冒號(hào)后可以添加對(duì)該標(biāo)記的簡(jiǎn)短描述,這對(duì)于團(tuán)隊(duì)協(xié)作和文檔化非常有幫助。
完成上述配置后,我們就可以利用 Pytest 的 -m 命令行選項(xiàng)來(lái)靈活地運(yùn)行或跳過(guò)帶有特定標(biāo)記的測(cè)試了。
運(yùn)行所有測(cè)試
不帶任何標(biāo)記篩選參數(shù)時(shí),Pytest 將運(yùn)行所有收集到的測(cè)試:
$ pytest -v ========================================= test session starts ========================================= platform linux -- Python 3.11.6, pytest-7.2.2, pluggy-1.0.0 -- /usr/bin/python3 cachedir: .pytest_cache rootdir: /home/lars/tmp/python, configfile: pytest.ini collected 2 items test_skip.py::test1 PASSED [ 50%] test_skip.py::test2 PASSED [100%] ========================================== 2 passed in 0.00s ==========================================
只運(yùn)行帶有 integration 標(biāo)記的測(cè)試
使用 -m integration 選項(xiàng),Pytest 將只選擇并運(yùn)行被 @integration 裝飾器標(biāo)記的測(cè)試:
$ pytest -v -m integration ========================================= test session starts ========================================= platform linux -- Python 3.11.6, pytest-7.2.2, pluggy-1.0.0 -- /usr/bin/python3 cachedir: .pytest_cache rootdir: /home/lars/tmp/python, configfile: pytest.ini collected 2 items / 1 deselected / 1 selected test_skip.py::test1 PASSED [100%] =================================== 1 passed, 1 deselected in 0.00s ===================================
只運(yùn)行不帶 integration 標(biāo)記的測(cè)試
使用 -m 'not integration' 選項(xiàng),可以運(yùn)行所有未被 integration 標(biāo)記的測(cè)試。注意 not integration 表達(dá)式需要用引號(hào)括起來(lái),以避免 shell 解析問(wèn)題。
$ pytest -v -m 'not integration' ========================================= test session starts ========================================= platform linux -- Python 3.11.6, pytest-7.2.2, pluggy-1.0.0 -- /usr/bin/python3 cachedir: .pytest_cache rootdir: /home/lars/tmp/python, configfile: pytest.ini collected 2 items / 1 deselected / 1 selected test_skip.py::test2 PASSED [100%] =================================== 1 passed, 1 deselected in 0.00s ===================================
從 Pytest 4.x 升級(jí)到 5.x+ 并解決 pytest.config 移除帶來(lái)的條件測(cè)試執(zhí)行問(wèn)題,最優(yōu)雅且推薦的方式是采用自定義標(biāo)記結(jié)合 -m 命令行選項(xiàng)。通過(guò)簡(jiǎn)單的兩步:重新定義裝飾器為 pytest.mark.your_marker 并在 pytest.ini 中注冊(cè)該標(biāo)記,即可實(shí)現(xiàn)對(duì)測(cè)試的精細(xì)化控制。這種方法不僅解決了兼容性問(wèn)題,還提升了測(cè)試管理的靈活性和可維護(hù)性,是 Pytest 升級(jí)過(guò)程中的一項(xiàng)重要實(shí)踐。
以上就是Pytest 5.x+ 升級(jí)指南:通過(guò)自定義標(biāo)記實(shí)現(xiàn)命令行條件測(cè)試運(yùn)行與跳過(guò)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊(cè)表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)