我想使用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)
时间: 2023-04-07 09:02:43 浏览: 73
这段程序中存在的逻辑问题是无法确定参数化的数据是否可以并行读取。因为在程序中只是使用了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个进程并行运行测试
```
这样就可以并行读取被参数化的数据,提高测试效率。
pytest -n=2 使用pytest.main
可以使用pytest.main()来运行pytest -n=2命令,其中-n=2表示使用2个进程并行运行测试用例。具体实现方法如下所示:
```python
import pytest
if __name__ == '__main__':
pytest.main(['-n=2'])
```
上述代码中,`pytest.main(['-n=2'])`表示运行pytest命令,并传入参数`-n=2`,表示使用2个进程并行运行测试用例。