移動自動化appium(2)- Allure報告使用詳解

6 Allure報告

6.1 Allure介紹

Allure是一個獨立的報告插件,生成美觀易讀的報告,比之前的html好看,目前支持的語言:JavaPHPRubyPythonC#

6.2 Allure安裝

pip3 install allure-pytest

(注意:這裡不要使用pytest-allure-adaptor

有一個幫助文檔可以參考:

https://docs.qameta.io/allure/#_about

生成Allure報告的命令:

pytest –alluredir report

執行完這條命令,case運行完畢後,會在當前目錄下生成一個report文件夾,裏面有一個json文件,就是生成的報告

 

pytest.ini中,之前的報告是這樣生成的:

addopts = -s –html=report/report.html –reruns 1

如果想用allure,那麼改成:

addopts = -s –alluredir report –reruns 1

直接在終端執行pytest就可以了

6.3 json報告轉為html報告

上面的命令執行後,生成的是json文件,json文件不那麼好看,需要將json轉成html,這裡要先安裝一個插件,步驟:

1、下載allure壓縮包,地址:

https://dl.bintray.com/qameta/generic/io/qameta/allure/allure/2.7.0/allure-2.7.0.zip

2、解壓後,將bin目錄配置到環境變量

3、進入report的上一級目錄,執行命令:allure generate report/ -o report/html –clean

執行上面的操作步驟後,在report文件夾下就多了一個html文件夾,裏面的index.html文件就是html報告。

 

生成html的時候遇到這樣的報錯:

Exception in thread “main” java.lang.UnsupportedClassVersionError: io/qameta/all

ure/CommandLine : Unsupported major.minor version 52.0

那麼可能是你的jdk版本太低了,可以打開cmd,輸入java -version,以及javac -version,看一下版本,如果低於1.8,換成1.8版本就好了。

jdk1.8版本下載地址:

https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

 

6.4 allure+pytest實戰操作

6.4.1 step用法

@allure.step(“”):

用來描述用例步驟的,簡單一點的用法,比如新建一個項目,框架如圖:

 

第一種用法:

在”F:pythonallureDemoscriptstest_demo.py”文件中,寫入下列代碼:

import allure      class TestLogin:        @allure.step("測試登錄成功的步驟")        def test_login_success(self):            print("login success")            assert 1

在終端輸入pytest運行測試用例,然後從cmd進入到F:pythonallureDemo這個路徑,輸入allure generate report/ -o report/html –clean,生成測試報告

觀察報告結果:

打開生成的報告,點擊左側的“包”或者“功能”,可以看到這樣的頁面,右側test_login_success是我們的測試用例,圈綠色的部分是step的內容。

 

第二種用法:

allure的幫助文檔中,給出的step的示例,test_steps_with_placeholders()這個測試用例:

import allure      @allure.step('Step with placeholders in the title, positional: "{0}", keyword: "{key}"')    def step_with_title_placeholders(arg1, key=None):        pass    def test_steps_with_placeholders():        step_with_title_placeholders(1, key='something')        step_with_title_placeholders(2)        step_with_title_placeholders(3, 'anything')

運行結果:

 

第三種用法:

第三種用法是只寫一個@allure.step,沒有參數,這種情況會直接把方法名放到步驟顯示的位置,例如:

  @allure.step        def test_login_fail1(self):            print("login fail1")            assert 0

運行結果:

 

 

 

還有比如:

@allure.step
def passing_step():
pass def test_with_step_in_fixture_from_conftest(fixture_with_conftest_step):   passing_step()

等等方式

6.4.2 attach()添加case描述和截圖

allure.attach()不是一個裝飾器,需要寫到test用例裏面,比如下面這樣:

    def test_login_fail(self):            allure.attach("輸入正確的用戶名:xxxx")            print("input username")            allure.attach("", "輸入錯誤的密碼:xxx")            print("input password")            allure.attach("", "點擊登錄")            print("click login button")            assert 0

第二個attach方法有兩個參數,第一個有一個,這樣寫雖然都不會報錯,但是結果會不一樣,如圖:

 

第一個,只寫了一個參數的,會像圖第一個圈紅的位置那樣,必須點開這個才會看到操作步驟,第二個和第三個,直接就可以看到操作步驟,更直觀。

 

attach還可以增加截圖

    def test_login_success(self):            allure.attach.file(r'D:xxxxx.jpg', '我是附件截圖的名字',                               attachment_type=allure.attachment_type.JPG)            print("login success")            assert 1

結果:

 

點擊截圖的名字,就可以看到截圖。

attach.file()第三個參數,還可以是allure.attachment_type.PNGallure.attachment_type.HTMLallure.attachment_type.TEXT等。

 

6.4.3 Descriptions

  description是顯示在描述位置的內容,可以描述一下你這個測試用例想做什麼。

 

第一種用法,長字符串:

@allure.description("""    Multiline test description.    That comes from the allure.description decorator.    Nothing special about it.    """)    def test_description_from_decorator():      assert 42 == int(6 * 7)

結果:

 

第二種用法,html格式:

可以使用html格式

 

import allure    @allure.description_html("""  <h1>Test with some complicated html description</h1>  <table style="width:100%">    <tr>      <th>Firstname</th>      <th>Lastname</th>      <th>Age</th>    </tr>    <tr align="center">      <td>William</td>      <td>Smith</td>      <td>50</td>    </tr>    <tr align="center">      <td>Vasya</td>      <td>Jackson</td>      <td>94</td>    </tr>  </table>  """)  def test_html_description():    assert True

結果:

 

這裡如果我們只是想要用一兩句話概括這個測試用例的話,直接用一個字符串就足夠了。

 

第三種用法,文檔注釋:

def test_unicode_in_docstring_description():        """Unicode in description.        Этот тест проверяет юникод.        你好夥計.        """        assert 42 == int(6 * 7)

像這樣的文檔注釋,會直接放到報告的描述中去,如圖:

 

6.4.4 title用法

  title就是顯示的case標題

比如這樣:

    @allure.title("case1:登錄成功的用例")        def test_login_success(self):            allure.attach.file(r'D:一寸照報名照片.jpg', '我是附件截圖的名字',                               attachment_type=allure.attachment_type.JPG)            print("login success")            assert 1

 

結果如圖:

 

會在圈紅部分兩個地方顯示。

如果有參數的話,還可以這樣用:

import allure  import pytest    @allure.title("Parameterized test title: adding {param1} with {param2}")  @pytest.mark.parametrize('param1,param2,expected', [      (2, 2, 4),      (1, 2, 5)  ])    def test_with_parameterized_title(param1, param2, expected):      assert param1 + param2 == expected

結果就是這樣:

 

6.4.5 link超鏈接

 

直接把官網的例子拿過來:

import allure    TEST_CASE_LINK = 'https://github.com/qameta/allure-integrations/issues/8#issuecomment-268313637'      @allure.link('https://www.youtube.com/watch?v=4YYzUTYZRMU')  def test_with_link():      pass      @allure.link('https://www.youtube.com/watch?v=Su5p2TqZxKU', name='Click me')  def test_with_named_link():      pass      @allure.issue('140', 'Pytest-flaky test retries shows like test steps')  def test_with_issue_link():      pass      @allure.testcase(TEST_CASE_LINK, 'Test case title')  def test_with_testcase_link():      pass

運行後,會出來一個鏈接,可以點擊。

6.4.6 用例級別

下面這段代碼示例里有兩個點,一個是feature方法,一個是severity

import allure      @allure.feature('這裡是一級標籤')  class TestLogin:      @allure.title("case1:登錄成功的用例")      @allure.description("這裡是對test_login_success用例的一些詳細說明")      @allure.story("這裡是第一個二級標籤")      def test_login_success(self):          allure.attach.file(r'D:一寸照報名照片.jpg', '我是附件截圖的名字',                             attachment_type=allure.attachment_type.JPG)          print("login success")          assert 1        @allure.title("case2:登錄失敗的用例")      @allure.description("這裡是對test_login_fail用例的一些詳細說明")      @allure.severity(allure.severity_level.CRITICAL)      @allure.story("這裡是第一個二級標籤")      def test_login_fail(self):          allure.attach("步驟1", "輸入正確的用戶名")          print("input username")          allure.attach("步驟2", "輸入錯誤的密碼")          print("input password")          allure.attach("步驟3", "點擊登錄")          print("click login button")          assert 0        @allure.title("case3:登錄失敗的用例")      @allure.description("這裡是對test_login_fail1用例的一些詳細說明")      @allure.severity(allure.severity_level.BLOCKER)      @allure.story("這裡是第一個二級標籤")      def test_login_fail1(self):          print("login fail1")          assert 0

點擊allure報告中的“圖表”,可以看到有優先級:

 

這個優先級,如果不寫,默認就是normal。用法就像標黃色底色那樣,級別總共有五個:

  • BLOCKER = ‘blocker’  中斷缺陷(客服端程序無響應,無法執行下一步驟)
  • CRITICAL = ‘critical’  臨界缺陷(功能點缺失)
  • NORMAL = ‘normal’  普通缺陷(數據計算錯誤)
  • MINOR = ‘minor’  次要缺陷(界面錯誤與ui需求不符)
  • TRIVIAL = ‘trivial’  輕微缺陷(必須項無提示,或者提示不規範) 

 

用例分級的方法:

@allure.feature(‘這裡是一級標籤‘)

@allure.story(“這裡是第一個二級標籤“)

 

對應的結果為:

 

6.4.7 添加截圖

3.7.6講到過截圖的方法get_screenshot_as_file(路徑)6.4.2講了attach上傳圖片的方法,兩個方法結合起來就可以在測試過程中截圖並且傳到報告上

    def test_login(self):          # 輸入手機號          self.login_page.input_tel("1851062653")          # 輸入密碼          self.login_page.input_pwd("sy123")          time.sleep(3)          # 點擊登錄          self.login_page.click_login()          # 截圖的方法          self.login_page.screenshot("login_success")          # 上傳到報告          allure.attach.file(r'.screenlogin_success.png', 'login_success',                             attachment_type=allure.attachment_type.PNG)        def screenshot(self, file_name):          self.driver.get_screenshot_as_file("./screen/" + file_name + ".png")