限制pytest中要执行的测试用例的数量 [英] Limit the number of test cases to be executed in pytest

查看:87
本文介绍了限制pytest中要执行的测试用例的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一点背景

  • 我正在与 Jenkins 一起执行我的测试用例,我现在正在与 Jenkins 一起做一些 POC.
  • 而且,就我而言,有 500 多个测试用例需要一个小时才能执行.
  • 我只想执行一个测试用例,只是为了知道我在执行 Jenkins POC 时没有犯任何错误.

有没有办法限制要执行的测试用例的数量?类似的东西..

Is there a way to limit number of test case to be executed ? something like..

  pytest -vv --limit 1

使用 conftest.py?

using conftest.py?

推荐答案

您可以通过多种方式限制测试数量.例如,您可以通过将其全名作为参数传递来执行单个测试:

You can limit the amount of tests in many ways. For example, you can execute a single test by passing its full name as parameter:

$ pytest tests/test_spam.py::TestEggs::test_bacon

将仅运行 tests/test_spam.py 模块中 TestEggs 类中的测试方法 test_bacon.

will run only the test method test_bacon in class TestEggs in module tests/test_spam.py.

如果您不知道确切的测试名称,可以通过执行来查找

If you don't know the exact test name, you can find it out by executing

$ pytest --collect-only -q

您可以结合使用这两个命令来执行有限数量的测试:

You can combine both commands to execute a limited amount of tests:

$ pytest -q --collect-only 2>&1 | head -n N | xargs pytest -sv

将执行第一个 N 个收集的测试.

will execute first N collected tests.

如果需要,您也可以自己实现 --limit 参数.示例:

You can also implement the --limit argument yourself if you want to. Example:

def pytest_addoption(parser):
    parser.addoption('--limit', action='store', default=-1, type=int, help='tests limit')


def pytest_collection_modifyitems(session, config, items):
    limit = config.getoption('--limit')
    if limit >= 0:
        items[:] = items[:limit]

现在上面的命令就等于

$ pytest --limit N

这篇关于限制pytest中要执行的测试用例的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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