How to specify more than one parameter in a pytest configuration [pytest_addoption]

out of curiosity, is it possible to add several commands on the command line for pytest? I see that conftest.py has pytest_addoption, but I am wondering how to add a few options. If anyone has a suggestion ... I poked, but could not find how to do it ... thanks!

+7
source share
2 answers

You can optionally specify many command line options with the pytest_addoption hook.

In the pytest protocol documentation :

Parameters: parser. To add command line options, call parser.addoption (...). To add ini values ​​to a file, call parser.addini (...).

The pytest_addoption hook pytest_addoption passed to the parser . You can add as many command line options as you want by calling parser.addoption(...) as many times as you want.

So, an example of adding two parameters is as simple as:

 def pytest_addoption(parser): parser.addoption('--foo', action='store_true', help='Do foo') parser.addoption('--bar', action='store_false', help='Do not do bar') 

And, like any other hook.test, you need to go to the conftest.py file.

+9
source

in this way you can add an option as shown below:

 def pytest_addoption(parser): print('conftest method') parser.addoption("--hostip", action = "store", default = "127.0.0.1", help ="host ip address") parser.addoption("--port", action="store", default="5000", help="port") @pytest.fixture def get_param(request): config_param = {} config_param["host"] = request.config.getoption("--hostip") config_param["port"] = request.config.getoption("--port") return config_param 

and to run, which you can use:

 pytest -s <filename> --hostip=<value> --port=<value> 

hope this helps.

0
source

Source: https://habr.com/ru/post/1490564/


All Articles