變更標準 (Python) 測試探索

在測試收集期間忽略路徑

您可以透過在 cli 上傳遞 --ignore=path 選項,輕鬆地在收集期間忽略某些測試目錄和模組。pytest 允許多個 --ignore 選項。範例

tests/
|-- example
|   |-- test_example_01.py
|   |-- test_example_02.py
|   '-- test_example_03.py
|-- foobar
|   |-- test_foobar_01.py
|   |-- test_foobar_02.py
|   '-- test_foobar_03.py
'-- hello
    '-- world
        |-- test_world_01.py
        |-- test_world_02.py
        '-- test_world_03.py

現在,如果您使用 --ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/ 調用 pytest,您將看到 pytest 僅收集與指定模式不符的測試模組

=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 5 items

tests/example/test_example_01.py .                                   [ 20%]
tests/example/test_example_02.py .                                   [ 40%]
tests/example/test_example_03.py .                                   [ 60%]
tests/foobar/test_foobar_01.py .                                     [ 80%]
tests/foobar/test_foobar_02.py .                                     [100%]

========================= 5 passed in 0.02 seconds =========================

--ignore-glob 選項允許根據 Unix shell 風格的萬用字元忽略測試檔案路徑。如果您想排除以 _01.py 結尾的測試模組,請使用 --ignore-glob='*_01.py' 執行 pytest

在測試收集期間取消選擇測試

可以在收集期間透過傳遞 --deselect=item 選項來個別取消選擇測試。例如,假設 tests/foobar/test_foobar_01.py 包含 test_atest_b。您可以執行 tests/ 內的所有測試,除了 tests/foobar/test_foobar_01.py::test_a 之外,方法是使用 --deselect tests/foobar/test_foobar_01.py::test_a 調用 pytestpytest 允許多個 --deselect 選項。

保留從命令列指定的重複路徑

pytest 的預設行為是忽略從命令列指定的重複路徑。範例

pytest path_a path_a

...
collected 1 item
...

僅收集測試一次。

若要收集重複的測試,請在 cli 上使用 --keep-duplicates 選項。範例

pytest --keep-duplicates path_a path_a

...
collected 2 items
...

由於收集器僅適用於目錄,如果您兩次指定單個測試檔案,pytest 仍然會收集兩次,無論是否指定了 --keep-duplicates。範例

pytest test_a.py test_a.py

...
collected 2 items
...

變更目錄遞迴

您可以在 ini 檔案中設定 norecursedirs 選項,例如專案根目錄中的 pytest.ini

# content of pytest.ini
[pytest]
norecursedirs = .svn _build tmp*

這會告訴 pytest 不要遞迴到典型的 subversion 或 sphinx-build 目錄,或任何以 tmp 為字首的目錄。

變更命名慣例

您可以透過在您的 組態檔案 中設定 python_filespython_classespython_functions 來組態不同的命名慣例。以下是一個範例

# content of pytest.ini
# Example 1: have pytest look for "check" instead of "test"
[pytest]
python_files = check_*.py
python_classes = Check
python_functions = *_check

這會使 pytest 在符合 check_* .py glob 模式的檔案、類別中的 Check 字首,以及符合 *_check 的函數和方法中尋找測試。例如,如果我們有

# content of check_myapp.py
class CheckMyApp:
    def simple_check(self):
        pass

    def complex_check(self):
        pass

測試收集看起來會像這樣

$ pytest --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.ini
collected 2 items

<Dir pythoncollection.rst-207>
  <Module check_myapp.py>
    <Class CheckMyApp>
      <Function simple_check>
      <Function complex_check>

======================== 2 tests collected in 0.12s ========================

您可以透過在模式之間新增空格來檢查多個 glob 模式

# Example 2: have pytest look for files with "test" and "example"
# content of pytest.ini
[pytest]
python_files = test_*.py example_*.py

注意

python_functionspython_classes 選項對於 unittest.TestCase 測試探索沒有影響,因為 pytest 將測試案例方法的探索委派給 unittest 程式碼。

將 cmdline 參數解譯為 Python 套件

您可以使用 --pyargs 選項使 pytest 嘗試將參數解譯為 python 套件名稱,衍生其檔案系統路徑,然後執行測試。例如,如果您安裝了 unittest2,您可以輸入

pytest --pyargs unittest2.test.test_skipping -q

這將執行相應的測試模組。與其他選項一樣,透過 ini 檔案和 addopts 選項,您可以使此變更更持久

# content of pytest.ini
[pytest]
addopts = --pyargs

現在,簡單調用 pytest NAME 將檢查 NAME 是否作為可匯入的套件/模組存在,否則將其視為檔案系統路徑。

找出收集了什麼

您可以隨時查看收集樹狀結構,而無需像這樣執行測試

. $ pytest --collect-only pythoncollection.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.ini
collected 3 items

<Dir pythoncollection.rst-207>
  <Dir CWD>
    <Module pythoncollection.py>
      <Function test_function>
      <Class TestClass>
        <Function test_method>
        <Function test_anothermethod>

======================== 3 tests collected in 0.12s ========================

自訂測試收集

您可以輕鬆指示 pytest 從每個 Python 檔案中探索測試

# content of pytest.ini
[pytest]
python_files = *.py

但是,許多專案都會有一個 setup.py,他們不希望匯入它。此外,可能有些檔案只能由特定的 python 版本匯入。對於這種情況,您可以透過在 conftest.py 檔案中列出它們來動態定義要忽略的檔案

# content of conftest.py
import sys

collect_ignore = ["setup.py"]
if sys.version_info[0] > 2:
    collect_ignore.append("pkg/module_py2.py")

然後,如果您有一個像這樣的模組檔案

# content of pkg/module_py2.py
def test_only_on_python2():
    try:
        assert 0
    except Exception, e:
        pass

以及一個像這樣的 setup.py 虛擬檔案

# content of setup.py
0 / 0  # will raise exception if imported

如果您使用 Python 2 解譯器執行,那麼您會找到一個測試,並排除 setup.py 檔案

#$ pytest --collect-only
====== test session starts ======
platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
collected 1 items
<Module 'pkg/module_py2.py'>
  <Function 'test_only_on_python2'>

====== 1 tests found in 0.04 seconds ======

如果您使用 Python 3 解譯器執行,那麼一個測試和 setup.py 檔案都將被排除

$ pytest --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
configfile: pytest.ini
collected 0 items

======================= no tests collected in 0.12s ========================

也可以根據 Unix shell 風格的萬用字元忽略檔案,方法是將模式新增至 collect_ignore_glob

以下範例 conftest.py 在使用 Python 3 解譯器執行時,會忽略檔案 setup.py 以及所有以 *_py2.py 結尾的檔案

# content of conftest.py
import sys

collect_ignore = ["setup.py"]
if sys.version_info[0] > 2:
    collect_ignore_glob = ["*_py2.py"]

自 Pytest 2.6 以來,使用者可以透過將布林值 __test__ 屬性設定為 False,來阻止 pytest 探索以 Test 開頭的類別。

# Will not be discovered as a test
class TestClass:
    __test__ = False