![](/img/trans.png)
[英]How can I specify a configuration for setting up pytest fixtures when I start the pytest test runner?
[英]How can I see the current pytest configuration?
pytest 從各種文件和命令行選項收集配置設置,但似乎沒有命令行選項來顯示 pytest 的當前設置,而且我不知道如何輕松地詢問 pytest 以獲取此信息。 pytest --help
(或pytest -h
)顯示可用選項是什么,但不顯示它們的當前值。
我試過在設置 PYTEST_DEBUG 的情況下運行PYTEST_DEBUG
(例如$ env PYTEST_DEBUG=1 pytest --setup-only
):這會生成大量調試信息,但它似乎不包含配置設置,或者至少不包含在易消化的格式。
我看到有一個Config
object 但我不知道如何詢問它以編寫一個小程序到 output 它的內容(我認為它需要比我擁有的更高級別的 pytest-fu); 我認為這個 object 可能是正確的地方,假設沒有命令行選項來顯示我錯過的當前設置。
查看 pytest 文檔后,似乎沒有直接的方法自動枚舉所有配置選項。 這里有一些獲取配置值的方法,希望這些方法對您有所幫助。
如果您有特定的單元測試並且知道特定的值,則 pytest 文檔中有一個示例顯示如何診斷是否設置了這些值。
此外,配置文檔描述了配置文件搜索和優先級。 pytest 的配置設置來自多個位置,包括 cmd 行選項、ini 文件和環境變量。
pytestconfig 的pytestconfig
是Config
的一部分,在文檔中進行了描述。
import pytest
import os
def test_answer(pytestconfig):
if pytestconfig.getoption("verbose") > 0:
print("verbose")
print(pytestconfig.inipath)
print(pytestconfig.invocation_params.args)
print(os.getenv('PYTEST_ADDOPTS', None))
print(pytestconfig.getini('addopts'))
assert 0 # to see what was printed
現在,如果我使用pytest test_sample.py
(沒有 cmd 行參數)在我的目錄中運行它。 上面給出了test_sample.py
的內容。 pytest.ini
文件內容為
[pytest]
addopts = -vq
並且PYTEST_ADDOPTS
未設置我看到:
---------------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------------
/Users/rlucas/Desktop/pytest.ini
('test_sample.py',)
None
['-vq']
================================================================================================== short test summary info ===================================================================================================
FAILED test_sample.py::test_answer - assert 0
並使用不同的調用pytest test_sample.py --verbose
你會看到
---------------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------------
verbose
/Users/rlucas/Desktop/pytest.ini
('test_sample.py', '--verbose')
None
['-vq']
================================================================================================== short test summary info ===================================================================================================
在這里,我將 output 縮寫為相關信息(例如,我沒有顯示測試失敗信息)。 如果您無法直接訪問運行單元測試的文件系統,您始終可以讀取在pytestconfig.inipath
中找到的文件並將內容打印到標准輸出。
受@Lucas Roberts 回答的啟發,我仔細研究了 pytest 的pytestconfig
夾具,它具有各種有趣的屬性,特別是option
(也稱為具有類似內容的known_args_namespace
)。 因此,如果我正確理解pytestconfig
,打印當前有效選項的粗略解決方案是,
# test_sample.py
import pytest
import os
def test_answer(pytestconfig):
for item_name in dir(pytestconfig.option):
if not item_name.startswith('_'):
print(f'{item_name}: {getattr(pytestconfig.option,item_name)}')
assert 0 # to see what was printed
然后按照 Lucas 建議的 pytest 運行它(例如,此處使用-vv
表示詳細程度 2),
$ pytest -vv test_sample.py
...
verbose: 2
version: False
xmlpath: None
==================== 1 failed in 0.10s ===================
$
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.