Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of generating HTML Test report and Optimization by Pytest

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of Pytest generating HTML test report and optimization method, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Pytest to generate HTML test report and optimization method article, let's take a look at it.

First, install plug-ins

To generate reports of type html, you need to use the pytest-html plug-in, which can be installed in IDE or on the command line. Plug-in installation

The location involves the use of different projects, which will not be detailed here. If you want to know, you can check it yourself.

Install in IDE

In the File > Settings > Project > Project Interpreter interface, click "+" to search for pytest-html to install.

Command line installation

It is recommended that you change to the "Lib\ site-packages" directory of the python installation path on the command line before executing the installation command.

Pip install-U pytest-html II. Generate html report

Prepare a simple execution script first

Import pytestdef fun (x): return x + 1def test_answer_1 (): "Test assertion one" assert fun (3) = = 4def test_answer_2 (): "Test assertion II" assert fun (5) = = 7@pytest.mark.parametrize ("test_input,expected", [("3i 5", 8), ("2th 4", 6), pytest.param ("6 * 9", 42 Mar marksprints pytest.mark.xfail) Pytest.param ("6 * 6", 42MagneMagnepytest.mark.skip)) def test_mark (test_input,expected): "Collection of use cases" assert eval (test_input) = = expectedif _ _ name__ = ='_ _ main__': pytest.main (['- vaunting dagger']])

Generate report command pytest-- html= report name the script file to be executed, execute the above script to view the results.

Report.html: report name, record when the report was generated, and plug-in version

Environment: test environment

Summary: use case statistics

Results: test results. Click Show all details / Hide all details to expand the results details or shrink all the results.

Third, use tips to specify the path

After running the script through the above command, you can find that the test report is saved in the root directory of the project, and it is cumbersome to find the report. We can

Specify the report path pytest-v-- html=./outputs/report.html test_08.py in the run command, and the code execution is complete

You can find that the outputs file is generated in the root directory of the project, and the test report is also in it.

Independent reporting

When the local execution is completed, I want to share the test report, only to find that the style of the shared report is lost when it is opened. Because the code execution is complete.

The assets file is generated and the CSS is saved locally. We can write CSS to HTML with the command so that the generated test report can

To share with the outside world.

Pytest-v-html=./outputs/report.html-self-contained-html test_ 08.PY IV, report optimization

In practical work, the test reports generated by the above operations are generally not the results we want. Environmental information is changed into demand through increase or decrease.

What to show, add use case descriptions, remove redundant columns, and so on. Here you need to write the optimization code to the conftest.py file, which is named solid

The order is irrevocable.

Import reference package

Import pytestfrom py._xmlgen import htmlfrom datetime import datetime

Modify the test environment

@ pytest.mark.parametrizedef pytest_configure (config): config._metadata.pop ("JAVA_HOME") # Delete java_home config._metadata ["Project name"] = "engine Automation" # add Project name config._metadata ["Interface address"] = "https://www.example.com/poke" # add Interface address

Modify use case statistics

@ pytest.mark.parametrizedef pytest_html_results_summary (prefix,summary,postfix): prefix.extend ([html.p ("affiliated department: test group")] prefix.extend ([html.p ("tester: Xu Weiling")])

Modify the result display

@ pytest.mark.optionalhookdef pytest_html_results_table_header (cells): cells.insert (1 link@pytest.mark.optionalhookdef pytest_html_results_table_row html.th ("Description")) # header add Description cells.insert (2 link@pytest.mark.optionalhookdef pytest_html_results_table_row html.th ("Time", class_= "sortable time", col= "time")) cells.pop (- 1) # Delete link@pytest.mark.optionalhookdef pytest_html_results_table_row (report,cells): cells.insert (1) Html.td (report.description)) # content corresponding to the header cells.insert (2Magi html.td (datetime.now (), class_= "col-time")) cells.pop (- 1) # Delete link@pytest.mark.hookwrapperdef pytest_runtest_makereport (item Call): # Description is the use case description _ _ doc__ outcome = yield report = outcome.get_result () report.description = str (item.function.__doc__) report.nodeid = report.nodeid.encode ("utf-8") .decode ("unicode_escape")

After the modification is completed, re-execute the script to see the final effect.

This is the end of the article on "Pytest generates HTML test reports and optimization methods". Thank you for reading! I believe you all have a certain understanding of "Pytest generates HTML test reports and optimization methods". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report