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

发布了26 篇原创文章 · 获赞 24 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/changyixue/article/details/97136350