Limit the number of test cases to be executed in pytest

前端 未结 1 1418
一生所求
一生所求 2021-01-21 16: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
1条回答
  •  隐瞒了意图╮
    2021-01-21 17:17

    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
    

    0 讨论(0)
提交回复
热议问题