Namratha
Namratha

Reputation: 91

pytest: How to define mandatory command line arguments

How can I define custom command line arguments, and make them mandatory in pytest?

For example, I want something like this:

pytest -s sample.py --test_suite='some_value'

And the command line argument --test_suite has to be mandatory. If it's missing, I want to throw a "help" message like, "enter the test_suite to be executed".

This is my code:

@pytest.fixture
def testsuite(request):
    test_suite = request.config.getoption('--test_suite')
    return test_suite

def pytest_addoption(parser):
    parser.addoption("--test_suite",action="store",default="default_suite",
                     help="Enter the test suite you want to execute."
                          "Ex. --test_suite=default_suite"
                          "If nothing is selected, default_suite tests will be run.")   

This approach makes the command line arguments optional. But I want to make them mandatory.

Upvotes: 7

Views: 2504

Answers (1)

Mike G
Mike G

Reputation: 668

To mark a command line argument as mandatory using addoption, add required=True as a keyword argument to the method call

Upvotes: 6

Related Questions