ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytest+yaml+allure接口自动化测试框架07.集成allure报告

2022-02-07 14:02:27  阅读:216  来源: 互联网

标签:body 07 attach yaml allure str key name


前言

​ 好久不见,在这里先祝大家新的一年里万事如意,事业节节高升!我已经拖了很久没有把这个系列更新完成了,主要原因还是自己写作的欲望在降低,对于我而言这是一个不好的信息。所以我必须得强迫自己把这个系列更新完成。

​ 之前我们整体上完成了整个框架的编写,这篇我就把allure报告也加入进来,测试报告的产出在测试框架中也是很重要的一环。

allure安装配置

allure的安装这里就不在说了,之前有一篇文章是已经说过了,
pytest使用allure

配置allure信息

安装好之后,我们先打开common/request.py文件,在里面做一下修改。

import allure

+++

    def send_request(self, **kwargs: t.Dict[t.Text, t.Any]) -> Response:
            response = self.dispatch(method, url, **request_data)
            description_html = f"""
            <font color=red>请求方法:</font>{method}<br/>
            <font color=red>请求地址:</font>{url}<br/>
            <font color=red>请求头:</font>{str(response.headers)}<br/>
            <font color=red>请求参数:</font>{json.dumps(kwargs, ensure_ascii=False)}<br/>
            <font color=red>响应状态码:</font>{str(response.status_code)}<br/>
            <font color=red>响应时间:</font>{str(response.elapsed.total_seconds())}<br/>
            """
            allure.dynamic.description_html(description_html)
            logger.info("Request Result: {}{}".format(response, response.text))
            return response

在执行请求的时候我们记录一下,该次请求的详情信息。
接着我们打开,common/result.py,更新一下处理结果文件的代码。

import allure

+++


def get_result(r, extract):
    """获取值"""
    for key in extract:
        value = get_var(key, r.text)
        logger.debug("正则提取结果值:{}={}".format(key, value))
        cache.set(key, value)
        pytest.assume(key in cache)
    with allure.step("提取返回结果中的值"):
        for key in extract:
            allure.attach(name="提取%s" % key, body=cache.get(key))


def check_results(r, validate):
    """检查运行结果"""
    expectcode = validate.get('expectcode')
    resultcheck = validate.get('resultcheck')
    regularcheck = validate.get('regularcheck')
    if expectcode:
        with allure.step("校验返回响应码"):
            allure.attach(name='预期响应码', body=str(expectcode))
            allure.attach(name='实际响应码', body=str(r.status_code))
        pytest.assume(expectcode == r.status_code)
    if resultcheck:
        with allure.step("校验响应预期值"):
            allure.attach(name='预期值', body=str(resultcheck))
            allure.attach(name='实际值', body=r.text)
        pytest.assume(resultcheck in r.text)
    if regularcheck:
        with allure.step("正则校验返回结果"):
            allure.attach(name='预期正则', body=regularcheck)
            allure.attach(name='响应值', body=str(
                re.findall(regularcheck, r.text)))
        pytest.assume(re.findall(regularcheck, r.text))

把上面这些工作加好之后,我们在命令行运行一下,带allure报告的cmd

pytest --html=report.html --self-contained-html --alluredir allure-results --clean-alluredir
allure generate allure-results -c -o allure-report
allure open allure-report

查看运行结果:
image

image

可以看到我们成功的把allure报告集成进来了,是不是很简单又很方便。

后记

新的一年新的展望,希望我今年能够坚持写博客!!!

标签:body,07,attach,yaml,allure,str,key,name
来源: https://www.cnblogs.com/wxhou/p/InterfaceYamlFramework07.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有