變更標準 (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_a
和 test_b
。您可以透過使用 --deselect tests/foobar/test_foobar_01.py::test_a
呼叫 pytest
,執行 tests/
中的所有測試,除了 tests/foobar/test_foobar_01.py::test_a
。 pytest
允許多個 --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_files
、python_classes
和 python_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-199>
<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
注意
對於 unittest.TestCase
測試發現,python_functions
和 python_classes
選項沒有作用,因為 pytest 會將測試案例方法的發現委派給 unittest 程式碼。
將命令列參數解釋為 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-199>
<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 ========================
也可以透過將模式新增到 collect_ignore_glob
來根據 Unix shell 風格的萬用字元忽略檔案。
以下範例 conftest.py
會忽略檔案 setup.py
,並在使用 Python 3 解譯器執行時忽略所有以 *_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