更改標准(Python)測試發現
在測試收集過程中忽略路徑
通過--ignore=path在cli上傳遞選項,可以輕松地在收集過程中忽略某些測試目錄和模塊。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
現在,如果你調用pytest使用,你會發現只收集測試模塊,這不符合指定的模式:--ignore=tests/foobar/test_foobar_03.py --ignore=tests/hello/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,執行pytest與--ignore-glob='*_01.py'。
測試期間收集的測試取消
通過傳遞--deselect=item選項,可以在收集過程中分別取消選擇測試。例如,說tests/foobar/test_foobar_01.pycontains test_a和test_b。您可以運行全部在測試tests/ 除了對tests/foobar/test_foobar_01.py::test_a 通過調用pytest帶。 允許多個選項。--deselect tests/foobar/test_foobar_01.py::test_apytest--deselect
保留從命令行指定的重復路徑
的默認行為pytest是忽略從命令行指定的重復路徑。例:
pytest path_a path_a
...
collected 1 item
...
只需收集一次測試。
要收集重復的測試,請使用--keep-duplicatescli上的選項。例:
pytest --keep-duplicates path_a path_a
...
collected 2 items
...
由於收集器僅適用於目錄,因此,如果您為單個測試文件指定兩次,則無論是否指定,pytest仍將收集兩次--keep-duplicates。例:
pytest test_a.py test_a.py
...
collected 2 items
...
更改目錄遞歸
你可以norecursedirs在ini文件中設置該選項,例如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"
# can also be defined in tox.ini or setup.cfg file, although the section
# name in setup.cfg files should be "tool:pytest"
[pytest]
python_files = check_*.py
python_classes = Check
python_functions = *_check
這將pytest在與glob-pattern 匹配的文件,類中的前綴以及match的函數和方法中尋找測試。例如,如果我們有:check_* .pyCheck*_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-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
collected 2 items
<Module check_myapp.py>
<Class CheckMyApp>
<Function simple_check>
<Function complex_check>
========================== no tests ran in 0.12s ===========================
你可以通過在模式之間添加空格來檢查多個glob模式:
# Example 2: have pytest look for files with "test" and "example"
# content of pytest.ini, tox.ini, or setup.cfg file (replace "pytest"
# with "tool:pytest" for setup.cfg)
[pytest]
python_files = test_*.py example_*.py
注意:在python_functions和python_classes選項有沒有效果unittest.TestCase測試發現,因為測試用例方法pytest代表發現到單元測試代碼。
將cmdline參數解釋為Python包
你可以使用該--pyargs選項pytest嘗試將參數解釋為python包名稱,派生其文件系統路徑,然后運行測試。例如,如果您安裝了unittest2,則可以鍵入:
pytest --pyargs unittest2.test.test_skipping -q
它將運行相應的測試模塊。與其他選項一樣,通過ini文件和addopts選項,您可以更永久地進行此更改:
# content of pytest.ini
[pytest]
addopts = --pyargs
現在,一個簡單的調用將檢查NAME是否作為可導入的程序包/模塊存在,否則將其視為文件系統路徑。pytest NAME
找出收集的東西
你始終可以查看集合樹,而無需運行如下測試:
. $ pytest --collect-only pythoncollection.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
collected 3 items
<Module CWD/pythoncollection.py>
<Function test_function>
<Class TestClass>
<Function test_method>
<Function test_anothermethod>
========================== no tests ran 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'>
====== no tests ran in 0.04 seconds ======
如果您使用Python 3解釋器運行,則一次測試和setup.py 文件都將被忽略:
$ pytest --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
collected 0 items
========================== no tests ran in 0.12s ===========================
通過向中添加模式,也可以忽略基於Unix Shell樣式通配符的文件collect_ignore_glob。
以下示例將conftest.py忽略該文件,setup.py並忽略*_py2.py使用Python 3解釋器執行時以結尾的所有文件:
# content of conftest.py
import sys
collect_ignore = ["setup.py"]
if sys.version_info[0] > 2:
collect_ignore_glob = ["*_py2.py"]