PyTest結合Allure管理測試用例並生成測試報告

一、安裝allure

1. 安裝allure-pytest,命令如下:(也可在pycharm中直接安裝)

    pip3 install allure-pytest

2.下載allure2:(下載後的安裝包名稱:allure-commandline-2.12.1.zip,解壓後爲:allure2-2.12.1)

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

3.把allure2(即:allure2-2.12.1文件夾)放到python的安裝目錄下的site-packages中(D:\python37\Lib\site-packages)

4.配置環境變量:

   path中加上如下路徑:D:\python37\Lib\site-packages\allure-2.12.1\bin

二、在測試用例腳本中應用allure

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
'''
caseName:工作日報
'''
# import unittest
import pytest
import allure
from businessView.daily_report import DailyReportPage
from baseView.browser_view import Browser
from common.get_configInfo import get_config
from businessView.login import LoginView
from common.get_log import get_log
log = get_log()

save_data = [("2019-07-03", "測試數據01", "0")]
update_data = [("2019-07-01", "測試數據-修改", "0"), ]


@allure.feature('工作日報')
class TestDailyReport(object):

    @pytest.fixture(scope="class")
    def init_dailyreport(self):
        # 打開瀏覽器和url
        browser = Browser()
        driver = browser.open_browser()

        # 登錄
        username = get_config("accountInfo", "username")
        password = get_config("accountInfo", "password")

        lv = LoginView(driver)
        lv.login_action(username, password)
        lv.into_report_page()

        # 初始化工作日報界面類
        drp = DailyReportPage(driver)
        yield drp

        # 以下相當於teardown代碼,不管用例執行成功與否,該代碼都會執行
        log.info("Close the browser.")
        driver.quit()

    @allure.story('暫存日報')
    @pytest.mark.parametrize("date,content,hours", save_data)
    def test_save(self, init_dailyreport, date, content, hours):
        log.info("******** 用例名稱:暫存日報 ********")
        init_dailyreport.save(date, content, hours)
        assert init_dailyreport.check_save()


if __name__ == "__main__":

    # 運行用例後生成測試數據(xml文件中)
    pytest.main(['-s', '-q', '--alluredir', '../report/xml', 'test_dailyreport.py'])

1.feature用來標註主要功能模塊

2.story用來標註單個用例或功能點

3.生成測試數據:(最後兩個參數分別是:放置數據的路徑和運行的腳本名稱)

pytest.main(['-s', '-q', '--alluredir', '../report/xml', 'test_dailyreport.py'])

4.生成測試報告:

allure generate 測試數據的路徑 -o 生成報告的路徑 --clean

例如:allure generate D:\webUIFrame\report\xml -o D:\webUIFrame\report\html --clean

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章