Reputation: 9099
Is there any way I can setup different testpath under pytest.init for my test. so I can executing something like pytest xx then all testcase can be executing from one set of multiple directies pytest yy then all testcase can be executing from another set of multiple directies pytest all then all testcase can be executing
so far I have. one set of test under unitA, unitB, unitC directies and another set of test under regressionA, regressionB, regressionC. so I do not need to type in pytest unitA, unitB, unitC pytest regressionA, regressionB, regressionC
my pytest.ini.
[pytest]
testpaths = unitA unitB
Upvotes: 1
Views: 2423
Reputation: 66581
Testpaths can be passed to pytest
as positional arguments. If you are on *nix, you can use shell glob expansion to match multiple directories: pytest unit*
will be expanded to pytest unitA unitB unitC
. Similarly, pytest unit{A,C}
will be expanded to pytest unitA unitC
.
However, you can also define own arguments if you need custom test filtering or grouping logic. Example for a switch --unit-only
that will run only tests in directories starting with unit
, ignoring the testpaths
setting in pytest.ini
:
# conftest.py
import pathlib
import pytest
def pytest_addoption(parser):
parser.addoption('--unit-only', action='store_true', default=False, help='only run tests in dirs starting with "unit".')
def pytest_configure(config):
unit_only = config.getoption('--unit-only')
if unit_only:
config.args = [p for p in pathlib.Path().rglob('unit*') if p.is_dir()]
Upvotes: 2