如何在 pytest 中仅运行未标记的测试 [英] How to run only unmarked tests in pytest

查看:71
本文介绍了如何在 pytest 中仅运行未标记的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 python 测试代码中有几个标记:

@pytest.mark.slowtest@pytest.mark.webtest@pytest.mark.stagingtest

我可以使用例如 pytest -m slowtest

的标记有选择地运行测试

如何在不求助于 pytest -m "not (slowtest or webtest or stagingtest)" 的情况下运行未标记的测试?

如您所想,我们将来可能会使用其他标记...

解决方案

我发现另一个有用的选项是使用 pytest.ini 添加默认选项.我们明确地写出要跳过的标记.它需要对 pytest.ini 中跳过的标记列表进行主线化.换句话说,不是 OP 要求的 100%,但这是我在寻找默认标记时发现的最好的 SO 问题.希望这可以帮助其他人.

来自文档:

<预><代码>添加选项将指定的 OPTS 添加到命令行参数集,就像它们已由用户指定一样.示例:如果您有这个 ini 文件内容:# pytest.ini 的内容[测试]addopts = --maxfail=2 -rf # 2次失败后退出,报告失败信息发布 pytest test_hello.py 实际上意味着:pytest --maxfail=2 -rf test_hello.py默认是不添加任何选项.

我添加到 pytest.ini 的内容

[pytest]addopts = -m 不慢";标记 =慢:将测试标记为慢(用-m不慢"取消选择)

使用它

﮸$ grep 'mark.slow' tests/*.py |wc -l3$ pytest -s======================================================== 测试会话开始 ======================================================================================================================================================================================================================================linux 平台——Python 3.8.2、pytest-6.0.1、py-1.9.0、pluggy-0.13.1根目录:/tests,配置文件:pytest.ini已收集 66 个项目/3 个取消选择/63 个选择../tests/test_app.py .....................................………………==================================================== 63已通过,8.70 秒取消选择 3 ==================================================

I have several markers in my python test code:

@pytest.mark.slowtest
@pytest.mark.webtest
@pytest.mark.stagingtest

I am able to selectively run tests with a marker using for example pytest -m slowtest

How can I run unmarked tests without resorting to pytest -m "not (slowtest or webtest or stagingtest)"?

As you can imagine, we might use other markers in the future...

解决方案

Another option I found useful is to use pytest.ini to add default options. We explicitly write which markers to skip. It entails mainlining the list of skipped markers in pytest.ini. In other words, not 100% what the OP asked for but this is the best SO question I found when looking for default markers. Hopefully, this can help others.

From the docs:


addopts

    Add the specified OPTS to the set of command line arguments as if they had been specified by the user. Example: if you have this ini file content:

    # content of pytest.ini
    [pytest]
    addopts = --maxfail=2 -rf  # exit after 2 failures, report fail info

    issuing pytest test_hello.py actually means:

    pytest --maxfail=2 -rf test_hello.py

    Default is to add no options.

What I added to my pytest.ini

[pytest]
addopts = -m "not slow"
markers =
    slow: marks tests as slow (deselect with '-m "not slow"')

Using it

﮸$ grep 'mark.slow' tests/*.py | wc -l
3

$ pytest -s
======================================================= test session starts ========================================================
platform linux -- Python 3.8.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /tests, configfile: pytest.ini
collected 66 items / 3 deselected / 63 selected                                                                                    

../tests/test_app.py ...............................................................

================================================= 63 passed, 3 deselected in 8.70s =================================================

这篇关于如何在 pytest 中仅运行未标记的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆