'Set `pytest` log options in `contest.py` based on a custom command line flag
I want to add a custom command-line flag to pytest which is responsible for setting multiple command-line options in conftest.py. (I don't want to set these options in pyproject.toml)
So basically, I want the following two commands too be equivalent
pytest -o log_cli=1 -o log_cli_level=10 -o log_format="%(message)s"
pytest --vmtrace
Now I have figured out how to register the --vmtrace option inside conftest.py as well as how I can access the value passed to it.
I am however, unable to figure out how to set the values of the log_cli log_cli_level and log_format options as required in the command above.
Here is what my conftest.py file currently looks like. Please suggest, what should replace the comments in the pytest_configure function.
def pytest_addoption(parser):
parser.addoption("--vmtrace", dest='vmtrace', default=1, action='store_const', const=10, help="Run trace")
def pytest_configure(config):
if config.getoption("vmtrace", default=1) == 10:
# Set the log_cli option to 1
# Set the log_cli_level option to 10
# Set the log_format option to "%(message)s"
Solution 1:[1]
Finally figured this one out. Posting here in case someone else needs this.
The solution seems to me to be a bit hacky but it works. log_cli seems to be unavailable here but the other two parameters can be set. The log_cli parameter is set to True when the log_cli_level is set.
def pytest_configure(config):
if config.getoption("vmtrace", default=1) == 10:
config.option.__dict__["log_cli_level"] = "10"
config.option.__dict__["log_format"] = "%(message)s"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | The Guru |
