allure框架:

提供一系列函数和类,主要用于生成更美观的测试报告,支持pytest,testng,jnuit

环境准备:

1.安装jdk并配置环境变量

JDK

2.安装allure并配置环境变量

allure-2.20.1.zip
image.png

3.安装allure-pytest
  1. pip install allure-pytest
  2. # 查看是否安装成功
  3. pip list
  4. # Package Version
  5. # --------------------- ---------
  6. # adbutils 0.11.0
  7. # alembic 1.9.1
  8. # alipay 0.7.4
  9. # allure-pytest 2.9.45 <---这个
  10. # allure-python-commons 2.9.45 <---这个
  11. # androguard 3.3.5
  12. # anyio 3.6.2
  13. # apkutils2 1.0.0
  14. # Appium-Python-Client 1.1.0
  15. # asn1crypto 1.5.1
  16. # asttokens 2.2.1

使用allure

  1. # 第一步:生成测试结果数据
  2. pytest.main(['--tb=line','--alluredir=allure-results','--clean-alluredir'])
  3. # --alluredir:用于指定allure测试结果数据的目录
  4. # --clean-alluredir:用于清除之前的数据
  5. # pytest负责执行测试,allure将opytest的测试结果转为allure测试结果
  6. # 会再目录下生成一个allure-result的目录,里面是一个json文件
  7. # 文件名:6203d740-b963-4d1a-b823-c039b28be9e9-result.json
  8. # 文件内容:{"name": "test_add", "status": "passed", "start": 1677310030228, "stop": 1677310030229, "uuid": "503138eb-b5a4-49da-94ad-ff54b32d195e", "historyId": "ca452a5adfea88d6ae47ca6a3c0df192", "testCaseId": "1cdd40043f5542f5e2b5336441273aff", "fullName": "cacle_test#test_add", "labels": [{"name": "suite", "value": "cacle_test"}, {"name": "host", "value": "LAPTOP-1HN2NVQ8"}, {"name": "thread", "value": "22296-MainThread"}, {"name": "framework", "value": "pytest"}, {"name": "language", "value": "cpython3"}, {"name": "package", "value": "cacle_test"}]}
  9. # 第二步:生成allure测试报告
  10. import os
  11. os.system('allure.bat generate allure-results -o report --clean')
  12. # generate生成报告
  13. # allure-results:allure测试结果数据目录
  14. # -o :导出测试报告
  15. # report:自定义的allure报告目录
  16. # --clean:清理之前的报告文件(必填)
  17. # 会生成一个名字为report的目录,点击index.html用浏览器打开即可

image.png

举例
  1. def add(a, b):
  2. return a + b
  3. def sub(a, b):
  4. return a - b
  5. if __name__ == 'main':
  6. pass
  1. import pytest
  2. from calc import *
  3. def test_add():
  4. case = ['测试两个整数相加', 2, 3, 5]
  5. case_info, a, b, exepct = case
  6. actual=add(a,b)
  7. assert actual==exepct,f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  8. if __name__ == '__main__':
  9. pytest.main(['--alluredir=allure-results','cacle_test.py'])
  10. import os
  11. os.system('allure.bat generate allure-results -o report --clean')

image.png

报告介绍

image.pngimage.png
image.pngimage.png
image.png

特性功能

作用:控制在测试报告中显示不同的内容

1.测试集特性
  1. @allure.suit('测试集名称')
  2. # 多个测试的集合,是对测试的归类
  3. # 如果不写测试集特性,则测试集默认为测试用例所在的模块名和目录名
  4. # 这个写法只能放在函数、方法、类之前
  5. # 不能使用参数化的参数
  1. import allure,pytest
  2. from calc import *
  3. @allure.suite('计算器测试')
  4. def test_add():
  5. case = ['测试两个整数相加', 2, 3, 5]
  6. case_info, a, b, exepct = case
  7. actual = add(a, b)
  8. assert actual == exepct, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  9. @allure.suite('计算器测试')
  10. def test_sub():
  11. case = ['测试两个整数相减', 2, 3, -2]
  12. case_info, a, b, exepct = case
  13. actual = sub(a, b)
  14. assert actual == exepct, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  15. if __name__ == '__main__':
  16. pytest.main(['--alluredir=allure-results', '--clean-alluredir', 'cacle_test02.py'])
  17. import o
  18. os.system('allure.bat generate allure-results -o report --clean')

image.png
image.png

2.层级特性
  1. @allure.epic('用例所属功能模块')
  2. # 一级功能
  3. # 只能放在函数、方法、类之前
  4. # 不能使用参数化的参数
  5. @allure.feature('用例所属功能')
  6. # 二级功能
  7. # 只能放在函数、方法、类之前
  8. # 不能使用参数化的参数
  9. @allure.story('用例的功能点或测试点')
  10. # 三级功能
  11. # 只能放在函数、方法、类之前
  12. # 不能使用参数化的参数
  13. # 关系演示
  14. |epic1
  15. |----feature1
  16. |--------story1
  17. |--------story2
  18. |--------story3
  19. |----feature2
  20. |epic2
  21. |----feature3
  22. |----feature4
  1. import allure, pytest
  2. from calc import *
  3. @allure.suite('计算器测试')
  4. @allure.epic('计算器加法计算')
  5. @allure.feature('测试加法功能')
  6. @allure.story('测试加法')
  7. def test_add():
  8. case = ['测试两个整数相加', 2, 3, 5]
  9. case_info, a, b, exepct = case
  10. actual = add(a, b)
  11. assert actual == exepct, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  12. @allure.suite('计算器测试')
  13. @allure.epic('计算器减法计算')
  14. @allure.feature('测试减法功能')
  15. @allure.story('测试减法')
  16. def test_sub():
  17. case = ['测试两个整数相减', 2, 3, -2]
  18. case_info, a, b, exepct = case
  19. actual = sub(a, b)
  20. assert actual == exepct, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  21. if __name__ == '__main__':
  22. pytest.main(['--alluredir=allure-results', '--clean-alluredir', 'cacle_test02.py'])
  23. import os
  24. os.system('allure.bat generate allure-results -o report --clean')
  25. # overview中的FEATURES BY STORY上,按照层级显示epic>featrue>story

image.png
image.png

3.添加日志
  1. import allure, pytest, logging
  2. from calc import *
  3. logging.basicConfig(level=logging.DEBUG)
  4. cases = [['测试两个整数相加', 2, 3, 5], ['测试两个整数相加', 2, 4, 6], ['测试两个整数相加', 3, 66, 6]]
  5. @allure.suite('计算器测试')
  6. @allure.epic('计算器加法计算')
  7. @allure.feature('测试加法功能')
  8. @allure.story('测试加法')
  9. @pytest.mark.parametrize('case_info,a,b,exepct', cases)
  10. def test_add(case_info, a, b, exepct):
  11. actual = add(a, b)
  12. if actual == exepct:
  13. passed, msg = True, f'{case_info},测试通过'
  14. logging.info(msg)
  15. else:
  16. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  17. logging.warning(msg)
  18. assert passed, msg
  19. sub_cases = [['测试两个整数相减', 2, 3, 5], ['测试两个负整数相加', -2, -4, -6], ['测试正数相减负数', 2, -3, 5]]
  20. @allure.suite('计算器测试')
  21. @allure.epic('计算器减法计算')
  22. @allure.feature('测试减法功能')
  23. @allure.story('测试减法')
  24. @pytest.mark.parametrize('case_info,a,b,exepct', sub_cases)
  25. def test_sub(case_info, a, b, exepct):
  26. actual = sub(a, b)
  27. if actual == exepct:
  28. passed, msg = True, f'{case_info},测试通过'
  29. logging.info(msg)
  30. else:
  31. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  32. logging.warning(msg)
  33. assert passed,msg
  34. if __name__ == '__main__':
  35. pytest.main(['-v', '--tb=line','--html=calc.html','--log-format=%(asctime)s [%(levelname)s] %(message)s [%(filename)s:%(lineno)s]','--log-date-format=%Y-%m-%d %H:%M:%S','--alluredir=allure-results', '--clean-alluredir', 'cacle_test03.py'])
  36. import os
  37. os.system('allure.bat generate allure-results -o report --clean')

image.png
image.png

4.用例特性
  1. @allure.title('用例标题')
  2. # 只能放在函数、方法、类之前
  3. # 可以使用参数化的参数
  4. # 每条用例只能执行一次
  5. @allure.description('用例描述')
  6. # 可以放在函数、方法前,但是不建议放到类之前
  7. # 可以使用参数化的参数
  8. # 每条用例只能执行一次
  9. # 大步骤
  10. @allure.step('测试步骤名')
  11. # 必须放在函数方法之前,不能放到类之前
  12. # 每条用例执行一次
  13. # 可以使用参数化的参数
  14. # 小步骤
  15. with allure.step('测试步骤')
  16. 具体代码步骤
  17. # 必须放在函数方法里面
  18. # 每条用例执行一次
  19. # 可以使用参数化的参数
  20. # 支持print、日志
  21. # 大步骤和小步骤至少有一个
  22. # 网页描述
  23. @allure.description_html(语言)
  24. # '<table border=1><tr><td>a<td>b<td>expect<tr><td>2<td>3<td>5</table>'
  1. import allure, pytest, logging
  2. from calc import *
  3. logging.basicConfig(level=logging.DEBUG)
  4. cases = [['测试两个整数相加', 2, 3, 5], ['测试两个整数相加', 2, 4, 6], ['测试两个整数相加', 3, 66, 6]]
  5. @allure.suite('计算器测试')
  6. @allure.epic('计算器加法计算')
  7. @allure.feature('测试加法功能')
  8. @allure.story('测试加法')
  9. @pytest.mark.parametrize('case_info,a,b,exepct', cases)
  10. @allure.title('测试标题:{case_info}')
  11. @allure.description('测试描述:测试计算器的功能,{case_info}')
  12. @allure.description_html('<table border=1><tr><td>a<td>b<td>expect<tr><td>2<td>3<td>5</table>')
  13. @allure.step('测试加法大步骤{case_info}')
  14. def test_add(case_info, a, b, exepct):
  15. with allure.step(
  16. """
  17. 调用被测函数,验证加法
  18. """
  19. ):
  20. actual = add(a, b)
  21. with allure.step(
  22. '断言'
  23. ):
  24. if actual == exepct:
  25. passed, msg = True, f'{case_info},测试通过'
  26. logging.info(msg)
  27. else:
  28. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  29. logging.warning(msg)
  30. assert passed, msg
  31. sub_cases = [['测试两个整数相减', 2, 3, 5], ['测试两个负整数相加', -2, -4, -6], ['测试正数相减负数', 2, -3, 5]]
  32. @allure.suite('计算器测试')
  33. @allure.epic('计算器减法计算')
  34. @allure.feature('测试减法功能')
  35. @allure.story('测试减法')
  36. @pytest.mark.parametrize('case_info,a,b,exepct', sub_cases)
  37. def test_sub(case_info, a, b, exepct):
  38. actual = sub(a, b)
  39. if actual == exepct:
  40. passed, msg = True, f'{case_info},测试通过'
  41. logging.info(msg)
  42. else:
  43. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  44. logging.warning(msg)
  45. assert passed, msg
  46. if __name__ == '__main__':
  47. pytest.main(['-v', '--tb=line', '--html=calc.html',
  48. '--log-format=%(asctime)s [%(levelname)s] %(message)s [%(filename)s:%(lineno)s]',
  49. '--log-date-format=%Y-%m-%d %H:%M:%S', '--alluredir=allure-results', '--clean-alluredir',
  50. 'cacle_test04.py'])
  51. import os
  52. os.system('allure.bat generate allure-results -o report --clean')

image.png
image.png
5.优先级特性

  1. @allure.serverity('用例优先级'或allure.serverity_level.优先级)
  2. # BLOCKER:中断,阻碍
  3. # CRITICL:严重
  4. # NORMAL:一般、正常、普通
  5. # MINOR:次要
  6. # TRIVIAL:轻微
  7. # 不能放在函数、方法之中
  8. # 所有用例只执行一次
  9. # 可以使用参数化的参数
  10. # 支持print、日志
  11. # 大步骤和小步骤应该至少有一个
  1. import allure, pytest, logging
  2. from calc import *
  3. logging.basicConfig(level=logging.DEBUG)
  4. cases = [['测试两个整数相加', 2, 3, 5], ['测试两个整数相加', 2, 4, 6], ['测试两个整数相加', 3, 66, 6]]
  5. @allure.suite('计算器测试')
  6. @allure.epic('计算器加法计算')
  7. @allure.feature('测试加法功能')
  8. @allure.story('测试加法')
  9. @pytest.mark.parametrize('case_info,a,b,exepct', cases)
  10. @allure.title('测试标题:{case_info}')
  11. @allure.description('测试描述:测试计算器的功能,{case_info}')
  12. @allure.step('测试加法大步骤{case_info}')
  13. @allure.severity('critical')
  14. def test_add(case_info, a, b, exepct):
  15. with allure.step(
  16. """
  17. 调用被测函数,验证加法
  18. """
  19. ):
  20. actual = add(a, b)
  21. with allure.step(
  22. '断言'
  23. ):
  24. if actual == exepct:
  25. passed, msg = True, f'{case_info},测试通过'
  26. logging.info(msg)
  27. else:
  28. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  29. logging.warning(msg)
  30. assert passed, msg
  31. sub_cases = [['测试两个整数相减', 2, 3, 5], ['测试两个负整数相加', -2, -4, -6], ['测试正数相减负数', 2, -3, 5]]
  32. @allure.suite('计算器测试')
  33. @allure.epic('计算器减法计算')
  34. @allure.feature('测试减法功能')
  35. @allure.story('测试减法')
  36. @pytest.mark.parametrize('case_info,a,b,exepct', sub_cases)
  37. def test_sub(case_info, a, b, exepct):
  38. actual = sub(a, b)
  39. if actual == exepct:
  40. passed, msg = True, f'{case_info},测试通过'
  41. logging.info(msg)
  42. else:
  43. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  44. logging.warning(msg)
  45. assert passed, msg
  46. if __name__ == '__main__':
  47. pytest.main(['-v', '--tb=line', '--html=calc.html',
  48. '--log-format=%(asctime)s [%(levelname)s] %(message)s [%(filename)s:%(lineno)s]',
  49. '--log-date-format=%Y-%m-%d %H:%M:%S', '--alluredir=allure-results', '--clean-alluredir',
  50. 'cacle_test05.py'])
  51. import os
  52. os.system('allure.bat generate allure-results -o report --clean')

image.png
image.png

5.链接特性
  1. @allure.issue('缺陷连接地址',name='报告中的链接文字')
  2. # 不能放在函数方法之中
  3. # 所有用例只执行一次
  4. # 不能使用参数化的参数
  5. @allure.testcase('用例链接地址',name='报告中的链接文字')
  6. # 不能放在函数方法之中
  7. # 所有用例只执行一次
  8. # 不能使用参数化的参数
  9. # 用例链接缺陷与缺陷链接先写的后显示,但是两个链接地址可以相同
  10. @allure.link('链接地址',name='报告中的链接文字')
  11. types.LinkType.ISSUE #表示缺陷链接
  12. types.LinkType.TEST_CASE #表示用例链接
  13. from allure_commons import type
  14. # 任意链接,不是非得报告
  15. # 不能放在函数方法中
  16. # 所有用例只执行一次
  17. # 不能使用参数化的参数
  1. import allure, pytest, logging
  2. from calc import *
  3. logging.basicConfig(level=logging.DEBUG)
  4. cases = [['测试两个整数相加', 2, 3, 5], ['测试两个整数相加', 2, 4, 6], ['测试两个整数相加', 3, 66, 6]]
  5. @allure.suite('计算器测试')
  6. @allure.epic('计算器加法计算')
  7. @allure.feature('测试加法功能')
  8. @allure.story('测试加法')
  9. @pytest.mark.parametrize('case_info,a,b,exepct', cases)
  10. @allure.title('测试标题:{case_info}')
  11. @allure.description('测试描述:测试计算器的功能,{case_info}')
  12. @allure.step('测试加法大步骤{case_info}')
  13. @allure.severity('critical')
  14. @allure.issue('http://www.baidu.com',name='缺陷地址')
  15. @allure.testcase('http://www.baidu.com',name='用例地址')
  16. def test_add(case_info, a, b, exepct):
  17. with allure.step(
  18. """
  19. 调用被测函数,验证加法
  20. """
  21. ):
  22. actual = add(a, b)
  23. with allure.step(
  24. '断言'
  25. ):
  26. if actual == exepct:
  27. passed, msg = True, f'{case_info},测试通过'
  28. logging.info(msg)
  29. else:
  30. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  31. logging.warning(msg)
  32. assert passed, msg
  33. sub_cases = [['测试两个整数相减', 2, 3, 5], ['测试两个负整数相加', -2, -4, -6], ['测试正数相减负数', 2, -3, 5]]
  34. @allure.suite('计算器测试')
  35. @allure.epic('计算器减法计算')
  36. @allure.feature('测试减法功能')
  37. @allure.story('测试减法')
  38. @pytest.mark.parametrize('case_info,a,b,exepct', sub_cases)
  39. def test_sub(case_info, a, b, exepct):
  40. actual = sub(a, b)
  41. if actual == exepct:
  42. passed, msg = True, f'{case_info},测试通过'
  43. logging.info(msg)
  44. else:
  45. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  46. logging.warning(msg)
  47. assert passed, msg
  48. if __name__ == '__main__':
  49. pytest.main(['-v', '--tb=line', '--html=calc.html',
  50. '--log-format=%(asctime)s [%(levelname)s] %(message)s [%(filename)s:%(lineno)s]',
  51. '--log-date-format=%Y-%m-%d %H:%M:%S', '--alluredir=allure-results', '--clean-alluredir',
  52. 'cacle_test05.py'])
  53. import os
  54. os.system('allure.bat generate allure-results -o report --clean')

image.png
image.png
6.动态特性

  1. # 动态特性必须放在函数或方法中,全都可以使用参数,来源于excel
  2. # 测试集
  3. allure.dynamic.suit('测试集名称')
  4. # 必须放在函数和方法汇总
  5. # 不建议,建议使用@allure.suite,否则报告中会显示混乱
  6. # 功能
  7. allure.dynamic.feature("功能")
  8. # 功能点
  9. allure.dynamic.story("功能点")
  1. import allure, pytest, logging
  2. from calc import *
  3. logging.basicConfig(level=logging.DEBUG)
  4. cases = [['计算器模块', '加法功能', '测试两个整数相加', 2, 3, 5], ['计算器模块', '加法功能', '测试两个整数相加', 2, 4, 6],
  5. ['计算器模块', '加法功能', '测试两个整数相加', 3, 66, 6]]
  6. @pytest.mark.parametrize('module,function,case_info,a,b,exepct', cases)
  7. def test_add(module, function, case_info, a, b, exepct):
  8. allure.dynamic.feature(case_info)
  9. allure.dynamic.story(function)
  10. with allure.step("调用被测函数,验证加法"):
  11. actual = add(a, b)
  12. with allure.step('断言'):
  13. if actual == exepct:
  14. passed, msg = True, f'{case_info},测试通过'
  15. logging.info(msg)
  16. else:
  17. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  18. logging.warning(msg)
  19. assert passed, msg
  20. sub_cases = [['测试两个整数相减', 2, 3, 5], ['测试两个负整数相加', -2, -4, -6], ['测试正数相减负数', 2, -3, 5]]
  21. @pytest.mark.parametrize('case_info,a,b,exepct', sub_cases)
  22. def test_sub(case_info, a, b, exepct):
  23. actual = sub(a, b)
  24. if actual == exepct:
  25. passed, msg = True, f'{case_info},测试通过'
  26. logging.info(msg)
  27. else:
  28. passed, msg = False, f'{case_info},测试失败,预期结果为{exepct},实际结果为{actual}'
  29. logging.warning(msg)
  30. assert passed, msg
  31. if __name__ == '__main__':
  32. pytest.main(['-v', '--tb=line', '--html=calc.html',
  33. '--log-format=%(asctime)s [%(levelname)s] %(message)s [%(filename)s:%(lineno)s]',
  34. '--log-date-format=%Y-%m-%d %H:%M:%S', '--alluredir=allure-results', '--clean-alluredir',
  35. 'cacle_test07.py'])
  36. import os
  37. os.system('allure.bat generate allure-results -o report --clean')

image.png
image.png

独立报告

allure.open

  1. allure.open
  2. # pycharm执行测试
  3. # 进入cmd
  4. # 进入测试数据目录
  5. # 打开测试报告
  6. # allure open -p 端口号 测试报告目录
  7. # 打开测试报告
  8. allure open -p 端口号 测试报告目录
  9. # 需要看报告的任意使用浏览器访问即可

image.png
image.png

allure server

  1. # 重新生成并打开报告
  2. # 进入cmd
  3. # 进入测试报告目录所在目录
  4. allure server -p 端口号 测试数据所在目录

image.pngimage.png