首页 > 其他分享 >Allure09-附件

Allure09-附件

时间:2022-12-31 19:44:25浏览次数:35  
标签:case type allure attachment 附件 Allure09 id

附件

  • allure.attach('要显示的文本或html代码', name='附件标题', attachment_type=附件类型)
    • 只能写在函数或方法中,不能加@,不支持@allure.attach的写法
    • 用于向测试报告中写入附加信息,通常是一些测试数据(不能上传文件)
    • 附件标题用于表示显示的文件名,如果省略,则随机为文件命名
    • 此写法的常用附件类型有allure.attachment_type.TEXT(默认)、allure.attachment_type.HTML、allure.attachment_type.JSON
    • attach代码位置会影响附件能否正常显示,一般可以写到代码最后
  • allure.attach.file('要上传的含路径的文件名', name='附件标题', attachment_type=附件类型)
    • 用于向测试报告中插入截图、文件等
    • 此写法的常用附件类型有allure.attachment_type.TEXT(默认)、allure.attachment_type.HTML、allure.attachment_type.JSON、allure.attachment_type.PNG、allure.attachment_type.JPG等
    • 在报告中,会展示文件内容
    • attach代码位置会影响附件能否正常显示,一般可以写到代码最后
    • 如果代码中有日志输出,则allure会自动将日志写入到报告中

编写测试代码文件ceshi_calc_as04.py,测试add、sub函数,添加纯文本字符串附件、json字符串、html代码、图片

add_cases=pandas.read_excel('add_sub.xlsx').values.tolist()@pytest.mark.parametrize('case_id, case_title, module, function, a, b, expect, severity', add_cases)
def test_add(case_id, case_title, module, function, a, b, expect, severity):
    allure.dynamic.testcase('http://www.baidu.com/?id='+case_id, f'{case_id}-{case_title}-用例链接')
    allure.attach('add加法函数测试-字符串附件', '附件标题-附注-备注-注释说明', allure.attachment_type.TEXT)
    allure.attach('<body><center>网页代码</body>', 'html代码附件', allure.attachment_type.HTML)
    with allure.step('调用add被测函数'):
        actual=add(a, b)
    with allure.step('断言'):
        passed=actual==expect
        if not passed:
            allure.dynamic.issue('http://www.baidu.com/?case_id='+case_id, f'{case_id}-{case_title}-缺陷链接')
        assert passed, f'预期结果:{a} + {b} = {expect},实际结果:{a} + {b} = {actual}'

编写测试代码文件ceshi_calc_div04.py,测试div函数,添加日志文件、有缺陷时添加图片

def test_div(case_id, case_title, module, function, a, b, expect, severity):
    allure.dynamic.testcase('http://www.baidu.com/?case_id='+case_id, f'{case_id}-{case_title}-用例链接')
    with allure.step('调用被测div函数'):
        actual=div(a, b)
        logging.debug(f'调用被测函数div成功')
    with allure.step('断言'):
        passed=actual==expect
        if passed:
            logging.info(f'执行用例{case_id}-{case_title}==断言成功')
        else:
            allure.dynamic.issue('http://www.baidu.com/?id='+case_id, f'{case_id}-{case_title}-缺陷链接')
            allure.attach.file('allure.jpg', '缺陷截图', allure.attachment_type.JPG)
            logging.warning(f'执行用例{case_id}-{case_title}==断言失败')
        allure.attach.file('calc.log', 'calc测试日志', allure.attachment_type.TEXT)
        assert passed, f'预期结果:{a} + {b} = {expect},实际结果:{a} + {b} = {actual}'

小结

  • allure.attach
    • 可以向测试报告中添加纯字符串、json字符串、html代码等附件信息
  • allure.attach.file
    • 可以上传文件到测试报告中,比如缺陷截图
    • 如果代码中写有日志代码,allure会自动上传日志到报告中

标签:case,type,allure,attachment,附件,Allure09,id
From: https://www.cnblogs.com/sean-test/p/17017154.html

相关文章