Python自动化测试系列[v1.0.0][Allure测试报告]

环境配置

首先python环境中安装pytest和pytest_allure_adaptor模块,命令如下

pip install pytest
pip install pytest_allure_adaptor

执行测试

pytest -s -q --alluredir [path_to_report_dir]
  • [path_to_report_dir]:为执行测试后生成测试结果的路径,是个自定义路径
  • 测试完成后,在命令中指定的[path_to_report_dir]的自定义路径下,会生成类似如下xml文件,该文件中存下了执行测试后的测试结果,文件名类似为(自动生成):95060033-2085-40bb-b85a-64e43dc1d04b-testsuite.xml
<ns0:test-suite xmlns:ns0="urn:model.allure.qatools.yandex.ru" start="1544418468984" stop="1544418469065">
  <name>TestScriptsname>
  <labels/>
  <test-cases>
    <test-case start="1544418468984" status="passed" stop="1544418468987">
      <name>test_asserts.test_addname>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps>
        <step start="1544418468987" status="passed" stop="1544418468987">
          <name>test_addname>
          <title>test_addtitle>
          <attachments/>
          <steps/>
        step>
      steps>
    test-case>
    <test-case start="1544418468990" status="passed" stop="1544418468991">
      <name>test_asserts.test_add2name>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps>
        <step start="1544418468991" status="passed" stop="1544418468991">
          <name>test_add2name>
          <title>test_add2title>
          <attachments/>
          <steps/>
        step>
      steps>
    test-case>
    <test-case start="1544418468992" status="passed" stop="1544418468993">
      <name>test_asserts.test_add3name>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps>
        <step start="1544418468993" status="passed" stop="1544418468993">
          <name>test_add3name>
          <title>test_add3title>
          <attachments/>
          <steps/>
        step>
      steps>
    test-case>
    <test-case start="1544418468994" status="failed" stop="1544418469045">
      <name>test_asserts.test_add4name>
      <failure>
        <message>AssertionError: assert 39 >= 50
 +  where 39 = add(17, 22)message>
        <stack-trace>def test_add4():
>       assert add(17,22) >= 50
E       assert 39 >= 50
E        +  where 39 = add(17, 22)
 
test_asserts.py:34: AssertionErrorstack-trace>
      failure>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps/>
    test-case>
    <test-case start="1544418469046" status="passed" stop="1544418469047">
      <name>test_asserts.test_inname>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps/>
    test-case>
    <test-case start="1544418469048" status="passed" stop="1544418469049">
      <name>test_asserts.test_not_inname>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps/>
    test-case>
    <test-case start="1544418469050" status="failed" stop="1544418469054">
      <name>test_asserts.test_truename>
      <failure>
        <message>AssertionError: assert not True
 +  where True = is_prime(7)message>
        <stack-trace>def test_true():
>       assert not is_prime(7)
E       assert not True
E        +  where True = is_prime(7)
 
test_asserts.py:65: AssertionErrorstack-trace>
      failure>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps/>
    test-case>
    <test-case start="1544418469055" status="passed" stop="1544418469057">
      <name>test_fixture1.test_numbers_3_4name>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps/>
    test-case>
    <test-case start="1544418469058" status="passed" stop="1544418469059">
      <name>test_fixture1.test_strings_a_3name>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps/>
    test-case>
    <test-case start="1544418469060" status="passed" stop="1544418469062">
      <name>test_fixture2.TestUM.test_numbers_5_6name>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps/>
    test-case>
    <test-case start="1544418469064" status="passed" stop="1544418469065">
      <name>test_fixture2.TestUM.test_strings_b_2name>
      <attachments/>
      <labels>
        <label name="severity" value="normal"/>
        <label name="thread" value="9944-MainThread"/>
        <label name="host" value="DESKTOP-BE70S9D"/>
        <label name="framework" value="pytest"/>
        <label name="language" value="cpython3"/>
      labels>
      <steps/>
    test-case>
  test-cases>
ns0:test-suite>

使用allure-commandline将xml制作成html报告

  • 下载:allure2 allure1
  • 解压到allure-commandline目录
  • 进入bin目录,运行allure.bat
  • 添加allure到环境变量PATH(\安装路径\allure-commandline\bin)
    执行命令:
allure generate directory-with-results/ -o directory-with-report
  • 【directory-with-results】: 为第三部中的路径,第三步中已经指定
  • 【directory-with-report】: 自定义html生成到哪个路径下,如下图所示
    Python自动化测试系列[v1.0.0][Allure测试报告]_第1张图片

查看报告

Python自动化测试系列[v1.0.0][Allure测试报告]_第2张图片

报告样式

Python自动化测试系列[v1.0.0][Allure测试报告]_第3张图片
Python自动化测试系列[v1.0.0][Allure测试报告]_第4张图片
Python自动化测试系列[v1.0.0][Allure测试报告]_第5张图片

生成报告方法封装

import os
from time import sleep
 
 
class GenerateAllureReport:
 
    def __init__(self, case_path, xml_path, report_path):
        self.case_path = case_path
        self.xml_path = xml_path
        self.report_path = report_path
 
    def run(self):
        generate_xml = "pytest " + self.case_path + " -s -q --alluredir " + self.xml_path
        os.system(generate_xml)
        sleep(10)
        generate_report = "allure generate " + self.xml_path + " -o " + self.report_path + " --clean"
        os.system(generate_report)
        sleep(10)
 
 
if __name__ == '__main__':
    GenerateAllureReport("D://PythonPrograms/Python_Pytest/TestScripts", "D://PythonPrograms/Python_Pytest//TestResult",
                             "D://PythonPrograms/Python_Pytest//TestResult//Report").run()

你可能感兴趣的:(Python自动化测试系列[v1.0.0][Allure测试报告])