问题
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