Search code examples
pytestfixtures

pytest get command line options


I have couple of questions regarding commandline options of pytest

  1. If it is possible to use long names and short names for the same option, for example

    parser.addoption('--server', '-srv', dest = 'SERVER')

  2. How to access to commandline option by name, like :

    config.option.NAME

     def environment_options(parser):
         parser.addoption('--server', dest= "SERVER")
    
     @pytest.fixture()
     def enfironment_set_up():
         if config.option.SERVER == 'some value': 
            actions
    

pycharm shows reference unresolved 'config'. Do I need to import something ?


Solution

  • As far as I know (haven't found that in the documentation), it is possible to add a short name, but only one with one upper case letter, e.g.:

     def environment_options(parser):
         parser.addoption('-S', '--server', dest= "SERVER")
    

    Lowercase letters are reserved for pytest itself, and longer abbreviations are not supported. See also my somewhat related answer.

    You can access the option via config in the request fixture:

     @pytest.fixture
     def enfironment_set_up(request):
         if request.config.getoption("SERVER") == 'some value': 
            actions