1. 前言

  • 参数化 @pytest.mark.parametrize 的学习 9. 参数化 @pytest.mark.parametrize
  • 默认 allure 报告上的测试用例标题不设置默认就是用例名称,这样可读性不高
  • 当结合 @pytest.mark.parametrize 参数化完成数据驱动时,如果标题写死,这样可读性也不高
  • 所以我们希望标题可以动态的生成,来看看如何做吧

2. 参数化无标题的栗子

2.1 测试代码

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ =
  5. __Time__ = 2020/10/28 15:08
  6. __Author__ = 小菠萝测试笔记
  7. __Blog__ = https://www.cnblogs.com/poloyy/
  8. """
  9. import allure
  10. import pytest
  11. @pytest.fixture()
  12. def login(request):
  13. """登录"""
  14. param = request.param
  15. print(f"账号是:{param['username']},密码是:{param['pwd']}")
  16. # 返回
  17. return {"code": 0, "msg": "success!"}
  18. datas = [
  19. {"username": "name1", "pwd": "pwd1"},
  20. {"username": "name2", "pwd": "pwd2"},
  21. {"username": "name3", "pwd": "pwd3"}
  22. ]
  23. @allure.story('登录功能')
  24. @pytest.mark.parametrize('login', datas, indirect=True)
  25. def test_login1(login):
  26. """
  27. 登录测试用例1
  28. """
  29. assert login['code'] == 0

2.2 allure 报告

image.png
标题就是方法名+参数化的数据,看着可读性就不咋滴

3. 参数化有标题写死的栗子

3.1 测试代码

将上面的测试代码添加一个 @allure.title 就可以了

  1. @allure.story('登录功能')
  2. @allure.title('登录测试用例2')
  3. @pytest.mark.parametrize('login', datas, indirect=True)
  4. def test_login2(login):
  5. """
  6. 登录测试用例2
  7. """
  8. assert login['code'] == 0

3.2 allure 报告

image.png
因为参数化可以生成三条用例,所以三条用例都用了同一个 title,可读性也不咋滴

4. 参数化使用 ids 的栗子

4.1 测试代码

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ =
  5. __Time__ = 2020/10/28 15:08
  6. __Author__ = 小菠萝测试笔记
  7. __Blog__ = https://www.cnblogs.com/poloyy/
  8. """
  9. import allure
  10. import pytest
  11. @pytest.fixture()
  12. def login(request):
  13. """登录"""
  14. param = request.param
  15. print(f"账号是:{param['username']},密码是:{param['pwd']}")
  16. # 返回
  17. return {"code": 0, "msg": "success!"}
  18. datas = [
  19. {"username": "name1", "pwd": "pwd1"},
  20. {"username": "name2", "pwd": "pwd2"},
  21. {"username": "name3", "pwd": "pwd3"}
  22. ]
  23. ids = [
  24. "username is name1,pwd is pwd1",
  25. "username is name2,pwd is pwd2",
  26. "username is name3,pwd is pwd3"
  27. ]
  28. @allure.story('登录功能')
  29. @pytest.mark.parametrize('login', datas, ids=ids, indirect=True)
  30. def test_login1(login):
  31. """
  32. 登录测试用例1
  33. """
  34. assert login['code'] == 0

4.2 allure 报告

image.png

5. 参数化动态生成标题的栗子

5.1 测试代码

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ =
  5. __Time__ = 2020/10/28 15:08
  6. __Author__ = 小菠萝测试笔记
  7. __Blog__ = https://www.cnblogs.com/poloyy/
  8. """
  9. import allure
  10. import pytest
  11. @pytest.fixture()
  12. def login(request):
  13. """登录"""
  14. param = request.param
  15. print(f"账号是:{param['username']},密码是:{param['pwd']}")
  16. # 返回
  17. return {"code": 0, "msg": "success!"}
  18. datas = [
  19. {"username": "name1", "pwd": "pwd1"},
  20. {"username": "name2", "pwd": "pwd2"},
  21. {"username": "name3", "pwd": "pwd3"}
  22. ]
  23. data2 = [
  24. ("name1", "123456"),
  25. ("name2", "123456"),
  26. ("name3", "123456")
  27. ]
  28. @allure.story('分别传值')
  29. @allure.title('登录测试用例2-账号是:{username}-密码是:{pwd}')
  30. @pytest.mark.parametrize('username,pwd', data2)
  31. def test_login1(username, pwd):
  32. """
  33. 登录测试用例1
  34. """
  35. print(username, pwd)
  36. @allure.story('字典参数化')
  37. @allure.title('登录测试用例2-{dict}')
  38. @pytest.mark.parametrize('dict', datas)
  39. def test_login2(dict):
  40. """
  41. 登录测试用例1
  42. """
  43. print(dict['username'], dict['pwd'])
  44. @allure.story('传值进fixture')
  45. @allure.title('登录测试用例2{login}')
  46. @pytest.mark.parametrize('login', datas, indirect=True)
  47. def test_login3(login):
  48. """
  49. 登录测试用例2
  50. """
  51. assert login['code'] == 0

5.2 allure 报告

image.png
传入的如果是一个字典则显示完整字典值

6. 参数化动态生成标题最优方案的栗子

6.1 测试代码

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ =
  5. __Time__ = 2020/10/28 15:08
  6. __Author__ = 小菠萝测试笔记
  7. __Blog__ = https://www.cnblogs.com/poloyy/
  8. """
  9. import allure
  10. import pytest
  11. data = [
  12. ("name1", "123456", "name1 登录成功"),
  13. ("name2", "123456", "name2 登录失败"),
  14. ("name3", "123456", "name3 登录成功")
  15. ]
  16. @allure.story('分别传值')
  17. @allure.title('登录测试用例-{title}')
  18. @pytest.mark.parametrize('username,pwd,title', data)
  19. def test_login1(username, pwd, title):
  20. """
  21. 登录测试用例1
  22. """
  23. print(username, pwd)

6.2 allure 报告

image.png

6.3 这种做法的优点

  • 可以自定义各式各样的标题
  • 单独一个值去维护标题值
  • 可读性比较好,容易维护