Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest: How to define mandatory command line arguments

Tags:

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.

like image 462
Namratha Avatar asked Dec 28 '18 02:12

Namratha


1 Answers

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

like image 72
Mike G Avatar answered Oct 11 '22 19:10

Mike G