我有一个长期测试,持续2天,我不想包括在通常的测试运行中.我也不想输入命令行参数,这会在每次通常的测试运行中取消选择它和其他测试.当我真正需要时,我宁愿选择默认取消选择的测试.我尝试将测试重命名test_longrun为longrun并使用该命令
py.test mytests.py::longrun
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
bin*_*unt 12
这是一种略有不同的方式。
用以下内容装饰你的测试@pytest.mark.longrun:
@pytest.mark.longrun
def test_something():
...
Run Code Online (Sandbox Code Playgroud)
此时,您可以运行除标有该标记的测试之外的所有内容-m 'not longrun'
$ pytest -m 'not longrun'
Run Code Online (Sandbox Code Playgroud)
或者如果您只想运行longrun标记的测试,
$ pytest -m 'longrun'
Run Code Online (Sandbox Code Playgroud)
但是,要在 中设置-m 'not longrun' 默认值pytest.ini,请将其添加到addopts:
[pytest]
addopts =
-m 'not longrun'
...
Run Code Online (Sandbox Code Playgroud)
如果你想运行所有测试,你可以这样做
$ pytest -m 'longrun or not longrun'
Run Code Online (Sandbox Code Playgroud)
Rol*_*ier 11
作为上述pytest_configure解决方案的替代,我找到了pytest.mark.skipif.
你需要把pytest_addoption()成conftest.py
def pytest_addoption(parser):
parser.addoption('--longrun', action='store_true', dest="longrun",
default=False, help="enable longrundecorated tests")
Run Code Online (Sandbox Code Playgroud)
并且您skipif在测试文件中使用.
import pytest
longrun = pytest.mark.skipif(
not pytest.config.option.longrun,
reason="needs --longrun option to run")
def test_usual(request):
assert false, 'usual test failed'
@longrun
def test_longrun(request):
assert false, 'longrun failed'
Run Code Online (Sandbox Code Playgroud)
在命令行中
py.test
Run Code Online (Sandbox Code Playgroud)
不会执行test_longrun(),但是
py.test --longrun
Run Code Online (Sandbox Code Playgroud)
也将执行test_longrun().
sax*_*sax 10
尝试装饰你的测试 @pytest.mark.longrun
在你的 conftest.py
def pytest_addoption(parser):
parser.addoption('--longrun', action='store_true', dest="longrun",
default=False, help="enable longrundecorated tests")
def pytest_configure(config):
if not config.option.longrun:
setattr(config.option, 'markexpr', 'not longrun')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2413 次 |
| 最近记录: |