Is there a way to add custom CLI arguments to pytest using the same syntax as would be used with the argparse module?

1.1k Views Asked by At

TLDR:

I want to extent pytest's cli and I know how that can be done using their own API, but I was just wondering if there was a way to do so using the argparse module directly (or its syntax at least) since I already have all the cli arguments I want to add written in the standard argparse module's syntax for another portion of my program.

Full Question:

I have about 20 CLI (command line interface) arguments/flags/options which I would like to be able to accept in a standard python module and when running tests by calling pytest.

I already have the standard python module setup to accept all the CLI arguments I need using the argparse module using syntax such as shown in the argparse documentation example:

# standard_python_module.py

import argparse

# Initialize argument
parser = argparse.ArgumentParser(description='Program Description.')
parser.add_argument('--my_int', metavar='INT', type=int, nargs='?', help='an integer', default=0)

# Parse/get value
args = parser.parse_args()
my_int = args.myint

I also know that you can create custom additional CLI arguments for use with pytest in conftest.py using syntax like that described here:

# conftest.py

import pytest

# Initialize argument
def pytest_addoption(parser):
    parser.addoption('--my_int', metavar='INT', type=int, nargs='?', help='an integer', default=0)

# Parse/get value
def setup(pytestconfig):
    my_int = pytestconfig.getoption("my_int")

For initializing/creating custom CLI arguments using argparse vs pytest there is only one slight difference that I am aware of. Argparse:

parser.add_argument('--my_int', metavar='INT', type=int, nargs='?', help='an integer', default=0)

Pytest:

parser.addoption('--my_int', metavar='INT', type=int, nargs='?', help='an integer', default=0)

For parsing/accessing the arguments and their respective values later there is a bit more of a difference. I hate to have to repeat nearly the exact same code again to setup all 20 custom CLI arguments for pytest, and I believe pytest uses argparse behind the scenes now, so I'm wondering if there's a good way to be able to reuse the code I have written for setting up the CLI arguments using the argparse module for setting up the custom CLI arguments for pytest. And if there is a way to reuse the parsing/accessing code as well, that would be awesome!

I tried using the argparse module in conftest.py directly and doing everything the same way I did with the standard python module, but that didn't work properly with pytest. I'm also using the allure reports module which adds its own custom CLI argument to pytest and when I used the method described above it no longer recognized the allure custom CLI argument, probably because I was redefining/overwriting the existing set of argparse CLI arguments.

Perhaps there's a way to access the underlying argparse.ArgumentParser instance that pytest uses?

I also thought about creating a wrapper class to wrap the pytest parser and add an additional function with the same name as the standard argparse function which simply calls the underlying renamed function, but I'm not exactly sure how to implement that, and that also doesn't address the problem of later parsing the arguments.

Possibly relevant links: pytest Config class documentation pytest Parser class documentation

0

There are 0 best solutions below