Pytest: how to override multiple arguments with same key with -o/--override-ini?

Created on 15 Jan 2020  路  8Comments  路  Source: pytest-dev/pytest

this is my pytest.ini

[pytest]
python_files = test_*
python_classes = Test*
python_functions = test_*
addopts = --tc-file=configurations/utenze/andrea-integration.ini --tc-file=configurations/puntamenti/integration.ini

there are two --tc-fle parameters used by https://pypi.org/project/pytest-testconfig/

i would like o overwrite theme using -o

i tried

pytest -o --tc-file=configurations/utenze/andrea-integration.ini --tc-file=configurations/puntamenti/legaltest.ini

i obtain

pytest: error: argument -o/--override-ini: expected one argument

i tried

pytest -o --tc-file=configurations/utenze/andrea-integration.ini -o --tc-file=configurations/puntamenti/legaltest.ini

i obtain the same result.

i can fix the issue creating different .ini file and use -c to load the configuration,
but how i can overwrite parameters with same key?

thanks.

config bug

All 8 comments

Sounds like a bug.

For reference:

def pytest_addoption(parser, env=os.environ):
    """ Define the command line options for the plugin. """
    group = parser.getgroup('test-config')
    group.addoption('--tc-file',
                    action='append',
                    dest='testconfig',
                    default=[env.get(env_opt)],
                    help="Configuration file to parse and pass to tests"
                            " [PY_TEST_CONFIG_FILE]")

append cli parameters cannot be overridden by the "ini override"

you need to add a own cli parameter which resets the list

Well, try:

pytest --tc-file=configurations/utenze/andrea-integration.ini --tc-file=configurations/puntamenti/legaltest.ini

-o is for ini options, and there it would be -o foo=bar -o foo=baz (but might not work for lists).

in order to understand what is going on,
in my conftest.py i put this

def pytest_configure(config):

    parameters = config.option.testconfig

    if parameters is not None:
        for param in parameters:
            if param is not None:
                print("parametro -> " + param)

my pytest.ini file contains

addopts = --tc-file=configurations/utenze/andrea-integration.ini --tc-file=configurations/puntamenti/integration.ini
my cli command is

pytest --tc-file=configurations/utenze/giordano-integration.ini --tc-file=configurations/puntamenti/integration.ini
running i obtain

parametro -> configurations/utenze/andrea-integration.ini
parametro -> configurations/puntamenti/integration.ini
parametro -> configurations/utenze/giordano-integration.ini
parametro -> configurations/puntamenti/integration.ini

so i understand there are 4 parameters loading : 2 in pytest.ini e 2 in the command line.

my use case is to keep a pytest.ini for running with pycharm and override configuration for continous integration running, selecting the configuration of several environment and users running tests.

so my use case can be solved in a different way , i only need o understand well how to use these parameters.

an argparse solution would be to have --reset-tc-file which resets the metavar

sorry i'm not able to understand : is this a bug o i'm doing something wrong?
thanks.

you are doing something wrong

pytest uses argparse under the hood (which imposes its limitations)
one needs to create a new action with a new cli parameter name to reset the list for a "append"

Related: https://github.com/pytest-dev/pytest/issues/5127 (but not a good solution by itself, just reminded me of it).

@andreabisello
You can also use an env var to specify the default - so instead of having a / multiple defaults in your config (addopts), maybe control it via that.

Closing the issue here. You might want to consider to follow up in the plugin's issue tracker if necessary.

Was this page helpful?
0 / 5 - 0 ratings