catalogue
1,pytest_report_teststatus custom test results
2,pytest_ generate_ Generate test cases parameterized by tests
1,pytest_report_teststatus custom test results
pytest_ report_ The teststatus (report, config) hook function returns the result category, short letters and detailed words of the status report.
The result category is the category that counts the results, such as "passed", "skipped", "error", or an empty string.
Short letters such as'., "s", "E" or empty strings are displayed during the test.
In detailed mode, detailed words are displayed as the test progresses, such as "PASSED","SKIPPED","ERROR" Or empty string.
Parameters:
-
Report -- the report object whose status you want to return.
-
config(_pytest.config.Config) -- pytest configuration object.
Create test_case.py file
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ import pytest @pytest.fixture() def my_setup(): print("Pre operation") assert 0 == 1 @pytest.fixture() def my_teardown(): yield print("Post operation") assert 0 == 1 def test_case1(my_setup): assert 1 == 1 def test_case2(): assert 0 == 1 def test_case3(): assert 1 == 1 def test_case4(): assert 1 == 0 def test_case5(my_teardown): assert 1 == 1
Command line execution command:
pytest test_case.py
Operation results:
The command line executes the pytest case. By default,. Represents the passed case, F represents the failed case, and E represents the abnormal case.
If you want to customize the test results, you can use pytest_report_teststatus hook function, write the function in the conf test.py file.
Create the conftest.py file
Customize the test result. As √, F as x, error of setup as 0, error of teardown as 1, skip skipped as/
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ def pytest_report_teststatus(report, config): if report.when == 'call' and report.passed: return (report.outcome, '√', 'passed') if report.when == 'call' and report.failed: return (report.outcome, 'x', 'failed') if report.when == 'setup' and report.failed: return (report.outcome, '0', 'error') if report.when == 'teardown' and report.failed: return (report.outcome, '1', 'error') if report.skipped: return (report.outcome, '/', 'skipped')
Execute the command again from the command line:
pytest test_case.py
Operation results:
Display the customized results in the console.
2,pytest_ generate_ Generate test cases parameterized by tests
pytest_generate_tests calls this hook function before the test case is parameterized, and generates test cases based on the test configuration or the parameter values specified in the class or module of the test function.
1. Create the conftest.py file
Customize the parameterized hook to judge that when the test case passes param parameters, the parameterized use case will be generated.
Check the test context of the request through the incoming metafunc object, and call the metafunc. Parameter () method for parameterization.
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ def pytest_generate_tests(metafunc): if "param" in metafunc.fixturenames: metafunc.parametrize("param", metafunc.module.case_data, ids=metafunc.module.case_id, scope="function")
2. Create test_case.py file
case_id is the use case ID, case_data is case data
Script code:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ The official account of WeChat: AllTests software test """ case_id = ["001", "002"] case_data = [{"url": "https://www.baidu.com/"}, {"url": "https://www.cnblogs.com/alltests/"}] def test_url(param): print("\n Parameters:" + str(param))
3. Open the command line and enter the execution command:
pytest -s -v test_case.py
Operation results:
3. More Hooks functions
Hooks hook functions fall into 6 categories:
-
Bootstrapping hooks - boot hooks that call plug-ins registered early enough (internal and setuptools plug-ins).
-
Initialization hooks - initialization hooks that call the plug-in and the conf test.py file.
-
Collection hooks - collection hooks. pytest calls the collection hooks to collect files and directories.
-
Test running (runtest) hooks - test running (runtest) hooks. All runtest related hooks receive a pytest.Item object.
-
Reporting hooks - report hooks.
-
Debugging/Interaction hooks - debug / interaction hooks. Few hooks can be used for special reporting or interaction with exceptions.
For details on the use of Hooks hook function, please refer to the official document:
https://docs.pytest.org/en/latest/reference/reference.html#hooks