Python unit testing framework pytest commonly used test report types

Table of contents

1.allure test report

2. Generate resultlog file

3. Generate JunitXML file

4. Generate the URL of the test case

5. Generate html test report

Summarize:


The previous blog introduced the installation and use of the pytest test framework. Now let’s talk about what test reports pytest can generate.

1.allure test report

2. Generate resultlog file

#!/usr/bin/python# -*- coding: UTF-8 -*-"""@author:chenshifeng@file:test_report.py@time:2021/01/27"""class TestReport:     def test_one(self):        x = "shifeng"        assert "feng" in x     def test_two(self):        x = "hello"        assert x == "hi"

Excuting an order:

 pytest test_report.py  --resultlog=./resultlog.txt

Specify the current path to generate resultlog.txt file, open the file, the content is as follows:

. reportdemo/test_report.py::TestReport::test_oneF reportdemo/test_report.py::TestReport::test_two self = <test_report.TestReport object at 0x7fd9c0a3eac0>      def test_two(self):         x = "hello" >       assert x == "hi" E       AssertionError: assert 'hello' == 'hi' E         - hi E         + hello  test_report.py:16: AssertionError

3. Generate JunitXML file

Excuting an order:

pytest test_report.py  --junitxml=./resultlog.xml

Also specify to generate the resultlog.xml file in the current directory, open the file as follows:

<?xml version="1.0" encoding="utf-8"?><testsuites>    <testsuite errors="0" failures="1" hostname="chenshifengdeMacBook-Pro.local" name="pytest" skipped="0" tests="2"               time="0.072" timestamp="2021-01-27T23:56:58.204464">        <testcase classname="reportdemo.test_report.TestReport" file="reportdemo/test_report.py" line="9"                  name="test_one" time="0.001"></testcase>        <testcase classname="reportdemo.test_report.TestReport" file="reportdemo/test_report.py" line="13"                  name="test_two" time="0.002">            <failure message="AssertionError: assert &apos;hello&apos; == &apos;hi&apos;  - hi  + hello">self = &lt;test_report.TestReport object at 0x7fa152b97790&gt;                 def test_two(self):                x = &quot;hello&quot;                &gt; assert x == &quot;hi&quot;                E AssertionError: assert &apos;hello&apos; == &apos;hi&apos;                E - hi                E + hello                 test_report.py:16: AssertionError            </failure>        </testcase>    </testsuite></testsuites>

What is the use of creating such an XML file? Mainly for the convenience of Jenkin or other continuous integration tools to read.

4. Generate the URL of the test case

Excuting an order:

pytest test_report.py  --pastebin=all


Copy the session-log test report link generated at the end of the print result to the browser:


Of course, you can also choose to only show fail test cases

 pytest test_class.py  --pastebin=failed

5. Generate html test report

Install pytest-html via pip

 pip install pytest-html  

Execute the command in the current directory of the code file

pytest test_report.py --html=./report.html


Specify to generate a report.html file in the current directory and open the test file:

Summarize:

Thanks to everyone who read my article carefully! ! !

 I personally sorted out some technical materials I have compiled in my software testing career in the past few years, including: e-books, resume modules, various job templates, interview books, self-study projects, etc. Everyone is welcome to leave a message in the comment area 333 to get it for free, don't miss it.

 

Guess you like

Origin blog.csdn.net/MXB_1220/article/details/131711896