1. 前言

  • @allure.title 和 @allure.description 都是装饰器,给测试用例提供标题和描述

22. allure 的特性之 @allure.description()、@allure.title() 的详细使用

  • 其实 allure 还提供了在测试用例执行过程中动态指定标题和描述等标签的方法
  • 如: allure.dynamic.description allure.dynamic.title

2. allure.dynamic 的源代码

  1. class Dynamic(object):
  2. @staticmethod
  3. def title(test_title):
  4. plugin_manager.hook.add_title(test_title=test_title)
  5. @staticmethod
  6. def description(test_description):
  7. plugin_manager.hook.add_description(test_description=test_description)
  8. @staticmethod
  9. def description_html(test_description_html):
  10. plugin_manager.hook.add_description_html(test_description_html=test_description_html)
  11. @staticmethod
  12. def label(label_type, *labels):
  13. plugin_manager.hook.add_label(label_type=label_type, labels=labels)
  14. @staticmethod
  15. def severity(severity_level):
  16. Dynamic.label(LabelType.SEVERITY, severity_level)
  17. @staticmethod
  18. def feature(*features):
  19. Dynamic.label(LabelType.FEATURE, *features)
  20. @staticmethod
  21. def story(*stories):
  22. Dynamic.label(LabelType.STORY, *stories)
  23. @staticmethod
  24. def tag(*tags):
  25. Dynamic.label(LabelType.TAG, *tags)
  26. @staticmethod
  27. def link(url, link_type=LinkType.LINK, name=None):
  28. plugin_manager.hook.add_link(url=url, link_type=link_type, name=name)
  29. @staticmethod
  30. def issue(url, name=None):
  31. Dynamic.link(url, link_type=LinkType.ISSUE, name=name)
  32. @staticmethod
  33. def testcase(url, name=None):
  34. Dynamic.link(url, link_type=LinkType.TEST_CASE, name=name)

2.1 重点

上面有的方法都能进行动态修改,如:

  1. allure.dynamic.feature
  2. allure.dynamic.link
  3. allure.dynamic.issue
  4. allure.dynamic.testcase
  5. allure.dynamic.story
  6. allure.dynamic.title
  7. allure.dynamic.description

3. title 的栗子

3.1 测试代码

  1. https://www.yuque.com/poloyy/nz6yd2/lf410h#y5Ihq

3.2 allure 报告

image.png

4. description 的栗子

4.1 测试代码

  1. def test_1():
  2. """
  3. 动态设置描述
  4. """
  5. print(123)
  6. allure.dynamic.description("动态描述")
  7. allure.dynamic.title("动态标题")

4.2 allure 报告

image.png
可以看到动态描述会覆盖动态设置描述

5. 结合 parametrize

5.1 测试代码

  1. data = [
  2. ("name1", "123456", "name1 登录成功"),
  3. ("name2", "123456", "name2 登录失败"),
  4. ("name3", "123456", "name3 登录成功")
  5. ]
  6. @pytest.mark.parametrize('username,pwd,title', data)
  7. def test_2(username, pwd, title):
  8. """
  9. 登录测试用例1
  10. """
  11. print(username, pwd)
  12. allure.dynamic.title(title)

5.2 allure 报告

image.png

6. 其他属性的栗子

6.1 测试代码

  1. def test_2():
  2. allure.dynamic.feature('动态feature')
  3. allure.dynamic.story('动态story')
  4. allure.dynamic.link("https://www.cnblogs.com/poloyy/p/1.html", '动态Link')
  5. allure.dynamic.issue("https://www.cnblogs.com/poloyy/p/2.html", '动态Issue')
  6. allure.dynamic.testcase("https://www.cnblogs.com/poloyy/p/3.html", '动态testcase')

6.2 allure 报告

image.png