1. 程式人生 > >Pytest學習(20)- allure之@allure.step()、allure.attach的詳細使用

Pytest學習(20)- allure之@allure.step()、allure.attach的詳細使用

## 一、@allure.step的用法 - 可以理解為我們編寫測試用例中的每一步操作步驟,而在allure中表示對每個測試用例進行非常詳細的步驟說明 - 通過 @allure.step(),可以讓測試用例在allure報告中顯示更詳細的測試過程 示例程式碼: ```python # -*- coding: utf-8 -*- # @Time : 2020/12/12 8:34 # @Author : longrong.lang # @FileName: test_allure.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang import allure @allure.step("開啟網站首頁") def open(): pass @allure.step("輸入賬號、密碼") def input_UsernameAndPassWord(): sendAndClickLogin("xiaoqiang", "1") @allure.step("輸入賬號、密碼{arg1},{arg2},並點選登入") def sendAndClickLogin(arg1, arg2): pass @allure.step("驗證登入過程") def test_login(): open() input_UsernameAndPassWord() ``` **測試用例在allure的顯示** ![](https://img2020.cnblogs.com/blog/718867/202012/718867-20201212101424508-414661666.png) ### 小結 - step(引數),引數就是標題,你傳什麼,在allure上的步驟名就顯示什麼 - 支援位置引數和關鍵字引數 {arg1},{arg2},可參考報告中“ 輸入賬號、密碼'xiaoqiang','1',並點選登入”處,如果函式的引數沒有匹配成功就會報錯 ## 二、allure.attach的用法 **作用:** allure報告還支援顯示許多不同型別的附件,可以補充測試結果;可根據自身情況進行調整 **語法:** allure.attach(body, name, attachment_type, extension) ### 引數列表 body:要顯示的內容(附件) name:附件名字 attachment_type:附件型別,是 allure.attachment_type 裡面的其中一種 extension:附件的副檔名(比較少用) allure.attachment_type提供了哪些附件型別? ![](https://img2020.cnblogs.com/blog/718867/202012/718867-20201212103709141-1043769096.png) **語法二:** allure.attach.file(source, name, attachment_type, extension) source:檔案路徑,相當於傳一個檔案 示例程式碼如下: ```python # -*- coding: utf-8 -*- # @Time : 2020/12/12 8:34 # @Author : longrong.lang # @FileName: test_allure.py # @Software: PyCharm # @Cnblogs :https://www.cnblogs.com/longronglang import allure @allure.step("開啟網站首頁") def open(): pass @allure.step("輸入賬號、密碼") def input_UsernameAndPassWord(): sendAndClickLogin("xiaoqiang", "1") @allure.step("輸入賬號、密碼{arg1},{arg2},並點選登入") def sendAndClickLogin(arg1, arg2): pass @allure.step("驗證登入過程") def test_login(): open() input_UsernameAndPassWord() # 新增附件 def test_attachments(): # 在測試報告中畫了一個html頁面 allure.attach('HTML頁面,HelloWorld! ', 'Attach with HTML type', allure.attachment_type.HTML) # 新增一個html附件 allure.attach.file('./report.html', attachment_type=allure.attachment_type.HTML) # 新增一個圖片附件 allure.attach.file('./demo.jpg', attachment_type=allure.attachment_type.JPG) ``` **在allure報告中展示如下:** ![](https://img2020.cnblogs.com/blog/718867/202012/718867-20201212105918329-14063857