設定

命令行選項和設定檔設定

您可以使用一般幫助選項,取得關於 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 使用,如果它們具有 [tool:pytest] 區段,也可以用於保存 pytest 設定。

# setup.cfg
[tool:pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
    tests
    integration

警告

除非是非常簡單的使用案例,否則不建議使用 setup.cfg.cfg 檔案使用的解析器與 pytest.initox.ini 不同,這可能會導致難以追蹤的問題。如果可能,建議使用後者檔案或 pyproject.toml 來保存您的 pytest 設定。

初始化:確定 rootdir 和 configfile

pytest 為每次測試執行確定一個 rootdir,這取決於命令行參數(指定的測試檔案、路徑)以及設定檔的存在。確定的 rootdirconfigfile 在啟動期間會作為 pytest 標頭的一部分印出。

以下是 pytest 使用 rootdir 的用途摘要

  • 在收集期間建構節點 ID;每個測試都會被分配一個唯一的節點 ID,該 ID 以 rootdir 為根目錄,並考慮完整路徑、類別名稱、函數名稱和參數化(如果有的話)。

  • 被插件用作儲存專案/測試執行特定資訊的穩定位置;例如,內部 快取 插件在 rootdir 中建立一個 .pytest_cache 子目錄,以儲存其跨測試執行的狀態。

rootdir 用於修改 sys.path/PYTHONPATH 或影響模組的導入方式。有關更多詳細資訊,請參閱 pytest 導入機制和 sys.path/PYTHONPATH

--rootdir=path 命令行選項可用於強制指定目錄。請注意,與其他命令行選項相反,--rootdir 不能與 addopts 內部 pytest.ini 一起使用,因為 rootdir 已用於尋找 pytest.ini

尋找 rootdir

以下是從 args 尋找 rootdir 的演算法

  • 如果在命令行中傳遞 -c,則使用它作為設定檔,並將其目錄作為 rootdir

  • 確定被識別為檔案系統中存在的路徑的指定 args 的共同祖先目錄。如果找不到此類路徑,則將共同祖先目錄設定為當前工作目錄。

  • 在祖先目錄和向上目錄中尋找 pytest.inipyproject.tomltox.inisetup.cfg 檔案。如果找到其中一個,它將成為 configfile,其目錄將成為 rootdir

  • 如果未找到設定檔,則從共同祖先目錄向上尋找 setup.py 以確定 rootdir

  • 如果未找到 setup.py,則在每個指定的 args 和向上目錄中尋找 pytest.inipyproject.tomltox.inisetup.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 檔案不包含 [tool.pytest.ini_options] 表格,也會將其視為 configfile(這是版本 8.1 中新增的功能)。

檔案會按照以上順序考慮。來自多個 configfiles 候選者的選項永遠不會合併 - 第一個匹配項獲勝。

設定檔也決定了 rootpath 的值。

Config 物件(可透過 hooks 或透過 pytestconfig fixture 存取)將隨後攜帶這些屬性

  • config.rootpath:已確定的根目錄,保證存在。它用作建構測試位址(“節點 ID”)的參考目錄,也可以被插件用於儲存每個測試執行的資訊。

  • config.inipath:已確定的 configfile,可能為 None(基於歷史原因,它被命名為 inipath)。

在版本 6.1 中新增:config.rootpathconfig.inipath 屬性。它們是舊版 config.rootdirconfig.inifilepathlib.Path 版本,它們的類型為 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 使用的語法高亮主題可以使用兩個環境變數進行自訂