@pytest.mark.parametrize如何并行化?
时间: 2024-05-28 11:09:12 浏览: 16
@ pytest.mark.parametrize 可以实现参数化测试,但是它并没有提供并行化功能。要实现并行化,可以使用 pytest-xdist 插件,它可以在多个进程或多台机器上并行运行测试用例。安装插件后,在运行 pytest 命令时加上 -n 参数指定进程数即可实现并行化,例如 "pytest -n 4" 表示使用 4 个进程并行运行测试用例。
相关问题
我想使用pytest -n 2 并行读取被参数化的数据,你帮我分析程序中存在的逻辑问题。程序如下:import pytest from common.calculation import * import logging.config import json path = os.path.dirname(__file__) logging.config.fileConfig(path + r'\common\logging.conf') # 获取日志配置文件的信息 logger = logging.getLogger('apilog') # apilog是logging类的对象logger的对外名字 # 读取测试文件 data_path = os.path.dirname(__file__) data_path = os.path.dirname(data_path) data_path = data_path + r'\test\flask' data_name = os.listdir(data_path) # 测试数据 listData = [{} for i in range(len(data_name))] for num in range(len(data_name)): with open(str(data_path) + '\\' + str(data_name[num]), 'r', encoding='utf-8') as data: try: listData[num] = json.load(data) except Exception as e: print("JSONDecodeError:接口外读取文件有误--------", str(data_name[num])) logger.exception(f'文件:{data_name[num]}读取有误。路径追溯:{e}') @pytest.mark.parametrize("input_json",listData) def test_case(input_json): print(input_json)
这段程序中存在的逻辑问题是无法确定参数化的数据是否可以并行读取。因为在程序中只是使用了pytest的-n 2参数来指定并行读取的数量,但并没有对参数化的数据进行处理。因此,需要进一步修改程序,使其能够正确地并行读取参数化的数据。
pytest使用参数化时,怎么并行读取被参数化的数据
可以使用pytest-xdist插件来实现并行读取被参数化的数据。该插件可以在多个进程中并行运行测试用例,从而提高测试效率。在使用参数化时,需要使用pytest.mark.parametrize装饰器来指定参数化的数据,例如:
```
import pytest
@pytest.mark.parametrize("test_input,expected_output", [("3+5", 8), ("2+4", 6), ("6*9", 54)])
def test_eval(test_input, expected_output):
assert eval(test_input) == expected_output
```
在运行测试时,可以使用-xdist选项来启用并行运行,例如:
```
pytest -n 3 # 启用3个进程并行运行测试
```
这样就可以并行读取被参数化的数据,提高测试效率。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)