組態¶
命令列選項和組態檔設定¶
您可以使用一般說明選項,取得命令列選項和 INI 風格組態檔中的值說明
pytest -h # prints options _and_ config file settings
這將顯示已安裝外掛程式所註冊的命令列和組態檔設定。
組態檔格式¶
許多 pytest 設定 可以設定在組態檔中,根據慣例,它位於儲存庫的根目錄中。
pytest 支援的組態檔快速範例
pytest.ini¶
pytest.ini
檔優先於其他檔,即使是空的。
或者,可以使用隱藏版本 .pytest.ini
。
# pytest.ini or .pytest.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
pyproject.toml¶
在版本 6.0 中新增。
pyproject.toml
在包含 tool.pytest.ini_options
表格時,會考慮進行組態。
# pyproject.toml
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
"tests",
"integration",
]
注意
人們可能會疑惑,為什麼是 [tool.pytest.ini_options]
而不是 [tool.pytest]
,就像其他工具一樣。
原因是 pytest 團隊打算在未來充分利用豐富的 TOML 資料格式進行組態,並保留 [tool.pytest]
表格供此用途。目前 ini_options
表格用作現有 .ini
組態系統和未來組態格式之間的橋樑。
tox.ini¶
tox.ini
檔是 tox 專案的組態檔,如果它們有 [pytest]
區段,也可以用來儲存 pytest 組態。
# tox.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
setup.cfg¶
setup.cfg
檔案是一般用途的設定檔,最初由 distutils
(現已棄用)和 setuptools 使用,也可以用於存放 pytest 設定,如果它們有 [tool:pytest]
區段。
# setup.cfg
[tool:pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
警告
不建議使用 setup.cfg
,除非是針對非常簡單的使用案例。 .cfg
檔案使用與 pytest.ini
和 tox.ini
不同的剖析器,這可能會導致難以追蹤問題。如果可能,建議使用後面的檔案或 pyproject.toml
來存放您的 pytest 設定。
初始化:決定 rootdir 和 configfile¶
pytest 為每個測試執行決定一個 rootdir
,這取決於命令列參數(指定的測試檔案、路徑)和設定檔的存在。已決定的 rootdir
和 configfile
會在啟動期間列印為 pytest 標頭的一部分。
以下是 pytest
使用 rootdir
的摘要
在收集期間建構 nodeids;每個測試會被指定一個唯一的 nodeid,其根目錄為
rootdir
,並考量到完整路徑、類別名稱、函式名稱和參數化(如果有)。由外掛程式用作存放專案/測試執行特定資訊的穩定位置;例如,內部 快取 外掛程式會在
rootdir
中建立.pytest_cache
子目錄,以存放其跨測試執行的狀態。
rootdir
並非用於修改 sys.path
/PYTHONPATH
或影響模組的匯入方式。更多詳細資訊,請參閱 pytest 匯入機制和 sys.path/PYTHONPATH。
--rootdir=path
命令列選項可用於強制指定目錄。請注意,與其他命令列選項相反,--rootdir
無法與 pytest.ini
內的 addopts
搭配使用,因為 rootdir
已用於尋找 pytest.ini
。
尋找 rootdir
¶
以下是從 args
尋找 rootdir 的演算法:
如果在命令列中傳入
-c
,請將其用作組態檔,並將其目錄用作rootdir
。針對指定為檔案系統中存在路徑的
args
,找出其共同祖先目錄。如果找不到此類路徑,則將共同祖先目錄設定為目前工作目錄。在祖先目錄及其上層目錄中尋找
pytest.ini
、pyproject.toml
、tox.ini
和setup.cfg
檔案。如果找到其中一個檔案,則該檔案將成為configfile
,而其目錄將成為rootdir
。如果找不到組態檔,請從共同祖先目錄向上尋找
setup.py
以找出rootdir
。如果找不到
setup.py
,請在每個指定的args
和向上尋找pytest.ini
、pyproject.toml
、tox.ini
和setup.cfg
。如果找到一個匹配項,它將成為configfile
,其目錄將成為rootdir
。如果找不到
configfile
,也沒有傳遞任何組態參數,請使用已確定的共同祖先作為根目錄。這允許在不屬於套件一部分且沒有任何特定組態檔案的結構中使用 pytest。
如果沒有提供 args
,pytest 會收集當前工作目錄下的測試,並從那裡開始確定 rootdir
。
只有在下列情況下,檔案才會與組態相符
pytest.ini
:將永遠相符並優先,即使是空的。pyproject.toml
:包含[tool.pytest.ini_options]
表格。tox.ini
:包含[pytest]
區段。setup.cfg
:包含[tool:pytest]
區段。
最後,如果找不到其他匹配項,pyproject.toml
檔案將被視為 configfile
,即使它不包含 [tool.pytest.ini_options]
表格(這已新增至 8.1
)。
檔案會按照上述順序考慮。絕不會合併來自多個 configfiles
候選項的選項 - 第一個匹配項獲勝。
組態檔案也決定 rootpath
的值。
Config
物件(可透過掛勾或 pytestconfig
固定裝置存取)隨後會傳送這些屬性
config.rootpath
:已確定的根目錄,保證存在。它用作建構測試地址(「節點識別碼」)的參考目錄,外掛程式也可以使用它來儲存每個測試執行資訊。config.inipath
:已確定的configfile
,可能是None
(它被命名為inipath
是基於歷史原因)。
6.1 版新增: config.rootpath
和 config.inipath
屬性。它們是 pathlib.Path
舊版 config.rootdir
和 config.inifile
的版本,類型為 py.path.local
,並且仍然存在以維持向後相容性。
範例
pytest path/to/testdir path/other/
將確定共同祖先為 path
,然後檢查組態檔如下
# first look for pytest.ini files
path/pytest.ini
path/pyproject.toml # must contain a [tool.pytest.ini_options] table to match
path/tox.ini # must contain [pytest] section to match
path/setup.cfg # must contain [tool:pytest] section to match
pytest.ini
... # all the way up to the root
# now look for setup.py
path/setup.py
setup.py
... # all the way up to the root
警告
自訂 pytest 外掛程式命令列引數可能包含路徑,例如 pytest --log-output ../../test.log args
。然後 args
是強制性的,否則 pytest 會使用 test.log 的資料夾來確定 rootdir(另請參閱 問題 #1435)。點 .
也可以用來參照目前的作業目錄。
內建組態檔選項¶
有關選項的完整清單,請參閱 參考文件。
語法突顯主題自訂¶
pytest 使用的語法突顯主題可以使用兩個環境變數自訂
PYTEST_THEME
設定要使用的 pygment 樣式。PYTEST_THEME_MODE
將此樣式設定為淺色或深色。