preface
As mentioned earlier, request is a built-in fixture of pytest. It is used to obtain the test context through request Config gets the configuration object.
The function of pytestconfig is the same as that of request Config is the same, which is to obtain the configuration object
pytestconfig source code analysis
pytestconfig is a built-in fixture used to get configuration objects pytestconfig source code
pytestconfig has two common methods
- . getoption() get command line parameters
- . getini() gets the parameters of the ini configuration file
@fixture(scope="session") def pytestconfig(request: FixtureRequest) -> Config: """Session-scoped fixture that returns the :class:`_pytest.config.Config` object. Example:: def test_foo(pytestconfig): if pytestconfig.getoption("verbose") > 0: ... """ return request.config
From the above source code, we can see that the actual return is request config
Use example
def test_foo(pytestconfig): if pytestconfig.getoption("verbose") > 0: ...
getoption() get command line parameters
The case of obtaining command line parameters has been mentioned many times before, and this is also the most widely used
# content of conftest.py # Author - Shanghai youyou QQ exchange group: 717225969 # blog address https://www.cnblogs.com/yoyoketang/ import pytest def pytest_addoption(parser): parser.addoption( "--cmdopt", action="store", default="type1", help="my option: type1 or type2" ) @pytest.fixture def cmdopt(pytestconfig): return pytestconfig.getoption("--cmdopt")
test case
# test_cmdopt.py import pytest # Author - Shanghai youyou QQ exchange group: 717225969 # blog address https://www.cnblogs.com/yoyoketang/ def test_answer_1(pytestconfig): type = pytestconfig.getoption("--cmdopt") print("Get command line parameters:%s" % type) def test_answer_2(cmdopt): print("Get command line parameters:%s" % cmdopt)
When the command line runs the use case, you can use the -- cmdopt parameter
>pytest -s test_cmdopt.py --cmdopt type2 ================== test session starts ======= platform win32 -- Python 3.6.6, pytest-4.5.0, py-1.9.0, pluggy-0.13.1 rootdir: D:\wangyiyun\web\cases\module2 rerunfailures-9.1, xdist-2.1.0 collected 2 items test_cmdopt.py Get command line parameters: type2 .Get command line parameters: type2 . ================== 2 passed in 0.06 seconds ==========
getini() from pytest Ini configuration file to get parameters
In the root directory of the project, a pytest. Exe is usually placed Ini write some configuration parameters
[pytest] log_cli = 1 addopts = -v -x
We want to read pytest Ini, you can use pytestconfig Getini(), using the example
# conftest.py # Author - Shanghai youyou QQ exchange group: 717225969 # blog address https://www.cnblogs.com/yoyoketang/ @pytest.fixture(autouse=True) def get_ini(pytestconfig): '''read ini configuration information''' # Read log_cli configuration log_cli = pytestconfig.getini('log_cli') print("Get markers : %s" % log_cli) addopts = pytestconfig.getini('addopts') print("Get addopts Configuration of:%s " % addopts)
After the command line is run, you can see the printed content
test_cmdopt.py::test_answer_1 Get markers : True Get db Configuration of:['-v', '-x'] Get command line parameters: type1 PASSED
The addopts parameter can change the default command line option. This parameter can be used instead when we enter a pile of instructions in cmd to execute the use case, so as to avoid repeated command tapping
For example, in the above configuration, when pytest is run on the command line, the default parameters will be brought, such as' pytest -v -x '
log_cli is the console real-time output log. You can set True and False, or 1 and 0
log_cli
log_cli is the console real-time output log. You can set True and False, or 1 and 0. The default is off (False)
When log_ When cli = 0 or the default False state, the command line inputs the pytest run case, and the console outputs the run results according to each module
collected 6 items test_1.py .. [ 33%] test_anothersmtp.py . [ 50%] test_cmdopt.py .. [ 83%] test_y.py . [100%] ============================== 6 passed in 1.80 seconds ==============================
When log_ Run the pytest command after cli = 1
collected 6 items test_1.py::test_answer_1 PASSED [ 16%] test_1.py::test_answer_2 PASSED [ 33%] test_anothersmtp.py::test_showhelo PASSED [ 50%] test_cmdopt.py::test_answer_1 PASSED [ 66%] test_cmdopt.py::test_answer_2 PASSED [ 83%] test_y.py::test_i PASSED [100%] ============================== 6 passed in 0.84 seconds ==============================
At this time, the report will display the results by each use case