一、摘要

本篇博文将介绍Python和Selenium进行自动化测试时,如何借助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>TestScripts</name>
  <labels/>
  <test-cases>
    <test-case start="1544418468984" status="passed" stop="1544418468987">
      <name>test_asserts.test_add</name>
      <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_add</name>
          <title>test_add</title>
          <attachments/>
          <steps/>
        </step>
      </steps>
    </test-case>
    <test-case start="1544418468990" status="passed" stop="1544418468991">
      <name>test_asserts.test_add2</name>
      <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_add2</name>
          <title>test_add2</title>
          <attachments/>
          <steps/>
        </step>
      </steps>
    </test-case>
    <test-case start="1544418468992" status="passed" stop="1544418468993">
      <name>test_asserts.test_add3</name>
      <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_add3</name>
          <title>test_add3</title>
          <attachments/>
          <steps/>
        </step>
      </steps>
    </test-case>
    <test-case start="1544418468994" status="failed" stop="1544418469045">
      <name>test_asserts.test_add4</name>
      <failure>
        <message>AssertionError: assert 39 &gt;= 50
 +  where 39 = add(17, 22)</message>
        <stack-trace>def test_add4():
&gt;       assert add(17,22) &gt;= 50
E       assert 39 &gt;= 50
E        +  where 39 = add(17, 22)

test_asserts.py:34: AssertionError</stack-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_in</name>
      <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_in</name>
      <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_true</name>
      <failure>
        <message>AssertionError: assert not True
 +  where True = is_prime(7)</message>
        <stack-trace>def test_true():
&gt;       assert not is_prime(7)
E       assert not True
E        +  where True = is_prime(7)

test_asserts.py:65: AssertionError</stack-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_4</name>
      <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_3</name>
      <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_6</name>
      <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_2</name>
      <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报告

下载地址为:

https://github.com/allure-framework/allure2/releases

https://github.com/allure-framework/allure1/releases

解压到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生成到哪个路径下,如下图所示

五、查看报告

 六、报告样式

还可以切换语言