08-pytest-fixture参数化params

简介: 08-pytest-fixture参数化params

前言  

  • 应用场景:在写自动化case时,数据与脚本分离,数据尽量不要在脚死

参数介绍

单条数据传参

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/10
3. # @Author  : 大海
4. import pytest
5. 
6. # 测试数据,存放在list
7. user_data = ["chrome", "firefox"]
8. 
9. 
10. @pytest.fixture(params=user_data)
11. def open_browser(request):
12. return request.param
13. 
14. 
15. def test_register(open_browser):
16. print("打开{}浏览器".format(open_browser))
17. 
18. 
19. if __name__ == '__main__':
20.     pytest.main(["-s", "test_10.py"])

多条数据传参

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/10
3. # @Author  : 大海
4. # @File    : test_12.py
5. 
6. import pytest
7. 
8. # 测试数据,存放在字典中,最外层必须是列表
9. user_data = [{'user': '大海', 'pwd': '12345'}, {'user': '小白', 'pwd': '67890'}]
10. 
11. 
12. @pytest.fixture(params=user_data)
13. def open_browser(request):
14. # request.param 可以接收传入的参数
15. print('这是param', request.param)
16. # 一个字典相当于一条参数,通过key获取字典中的值
17. print('用户名:{}'.format(request.param['user']))
18. print('密码:{}'.format(request.param['pwd']))
19. return request.param
20. 
21. 
22. def test_register(open_browser):
23. # open_browser 就是返回值 request.param
24. print('用户名:{} 密码:{}'.format(open_browser['user'], open_browser['pwd']))
25. 
26. 
27. if __name__ == '__main__':
28.     pytest.main(["-s", "test_12.py"])


相关文章
|
11月前
|
测试技术 数据处理
Pytest参数化详解-上
Pytest参数化详解-上
43 0
|
12月前
|
测试技术
32-pytest-内置fixture之request使用
32-pytest-内置fixture之request使用
|
11月前
|
测试技术 数据处理
Pytest参数化详解-下
Pytest参数化详解-下
74 0
|
11月前
|
测试技术 Python
pytest--fixture
pytest--fixture
|
12月前
|
测试技术
09-pytest-parametrize参数化
09-pytest-parametrize参数化
06-pytest-fixture的三种调用方式
06-pytest-fixture的三种调用方式
|
12月前
|
Web App开发 测试技术
10-pytest-parametrize中使用fixture
10-pytest-parametrize中使用fixture
|
测试技术 Python
pytest fixture装饰器
pytest fixture装饰器
|
测试技术
pytest学习和使用13-Pytest的fixture如何使用request传入参数?
pytest学习和使用13-Pytest的fixture如何使用request传入参数?
90 0