Limit the number of test cases to be executed in pytest

落爺英雄遲暮 提交于 2019-12-02 05:34:37

问题


A little background

  • I am executing my test cases with Jenkins, I am doing a little POC with Jenkins right now.
  • And, in my case, there are 500+ test cases which takes an hour to execute.
  • I want only one test case to be executed just to know I didn't make any mistakes while doing my Jenkins POC.

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

  pytest -vv --limit 1

or

using conftest.py?


回答1:


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

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

will execute first N collected tests.

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]

Now the above command becomes equal to

$ pytest --limit N


来源:https://stackoverflow.com/questions/56698210/limit-the-number-of-test-cases-to-be-executed-in-pytest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!