The use of pytest_addoption in pytest

illustrate

        In daily UI automation testing, multiple scenarios may be used, such as testing, pre-release, and production environments, which requires us to switch environments. If we need to specify different system configurations in different environments, each modification The framework code configuration is very unautomated and causes unnecessary trouble for testing.

In response to this situation, pytest provides a method: pytest_addoption registration parameter, this problem can be solved by using this registration parameter, and parameters can be passed when executing use cases and commands.

usage

pytest_addoption registration parameters are generally used in conjunction with the built-in fixture pytestconfig, pytest_addoption registers command line parameters, and reads the parameter values ​​through the pytestconfig configuration object. An example is as follows:

        

# Register the custom parameter cys to the configuration object
def pytest_addoption(parser):
    parser.addoption("--cys", action="store",
                     default='this cys Description of the default value of',
                     help="the command line arguments '--cys' add to pytest in configuration")


# Read the value of the custom parameter from the configuration object
@pytest.fixture(scope="session")
def cys(request):
    return request.config.getoption("--cys")

The use case runs:

# Print out the value of the custom parameter
@pytest.fixture(autouse=True)
def suite_1(cys):
    print('\n --cys The value of:', cys)


if __name__ == '__main__':
    # Use parameters
    pytest.main(['-s'])

It can be seen that although the suite_1 function does not assign parameters, it obtains the configuration object method through pytestconfig.

Parameter Description

At the same time, the meanings of some parameters of parser.addoption() are used as follows:
name: the name of the custom command line parameter, which can be: "foo", "-foo" or "--foo";
action: the basic type of action to take when this parameter is encountered on the command line;
nargs: the number of command line arguments that should be used;
const: constant values ​​required for certain operations and nargs selection;
default: The default value generated if the parameter is not on the command line.
type: the type to which the command line argument should be converted;
choices: a container of allowed values ​​for parameters;
help: a brief description of the function of the parameter;

Here are some commonly used parameters to introduce:

1. action="store": By default, only the value of the parameter is stored. The value is of any type, and at the same time, default can also be any type of value.

2. action = "apped", which stores a list value, and default is also a list value. pytest will put the value of the default default parameter and the values ​​of multiple custom parameters in a list, for example:
        

# Register the custom parameter cys to the configuration object
def pytest_addoption(parser):
    parser.addoption("--cys", action="append",
                     default=['this cys Description of the default value of'],
                     help="the command line arguments '--cys' add to pytest in configuration")


# Read the value of the custom parameter from the configuration object
@pytest.fixture(scope="session")
def cys(request):
    return request.config.getoption("--cys")


# Print out the value of the custom parameter
@pytest.fixture(autouse=True)
def suite_1(cys):
    print('\n --cys The value of:', cys)


if __name__ == '__main__':
    # Use parameters
    pytest.main(['-s','--cys=111','--cys=222'])

3. type: type type contains python data types, such as str,int,list,dict,float, etc. The default is str type. It is also noted here that if the type type is specified, the default should also specify the same data type.

# Register the custom parameter cys to the configuration object
def pytest_addoption(parser):
    parser.addoption("--cys", action="store",
                     default=888,
                     type = int,
                     help="the command line arguments '--cys' add to pytest in configuration")


# Read the value of the custom parameter from the configuration object
@pytest.fixture(scope="session")
def cys(request):
    return request.config.getoption("--cys")


# Print out the value of the custom parameter
@pytest.fixture(autouse=True)
def suite_1(cys):
    print('\n --cys The value of:', cys)


if __name__ == '__main__':
    # Use parameters
    pytest.main(['-s','--cys=111'])

4. The container for the allowed values ​​of the choices parameter, that is to say, the parameter value must be in this container, and an error will be reported if the value exceeds the given value, for example:

        

# Register the custom parameter cys to the configuration object
def pytest_addoption(parser):
    parser.addoption("--cys", action="store",
                     default=888,
                     choices=['python', 'java', 'c++'],
                     help="the command line arguments '--cys' add to pytest in configuration")


# Read the value of the custom parameter from the configuration object
@pytest.fixture(scope="session")
def cys(request):
    return request.config.getoption("--cys")


# Print out the value of the custom parameter
@pytest.fixture(autouse=True)
def suite_1(cys):
    print('\n --cys The value of:', cys)


if __name__ == '__main__':
    # Use parameters
    pytest.main(['-s','--cys=python'])

      

If, at this time, I pass a parameter that is not in choices, the operation will report an error:

 

Tags: Python pytest

Posted by mj99 on Sun, 22 May 2022 07:47:33 +0300