简体   繁体   English

pytest 根据YAML配置文件进行参数化

[英]pytest parametrize based on YAML configuration file

I would like to generate parameters for a test我想为测试生成参数

def test_project_run(prj_root, board_type, successful_output, test_input)

using the pytest framework.使用 pytest 框架。 I want the parameters to be based on data in a YAML file with all needed information我希望参数基于 YAML 文件中的数据,其中包含所有需要的信息

Zephyr:
  projects:
     - prj_root: "/home/ubuntu/nrf5340_threads"
       prj_name: "threads"
       test_input: []
       successful_output: ["Toggled led0:", "Toggled led1:"]
       boards_to_test_on: ["nrf5340", "mps2-an521"]
     - prj_root: "/home/ubuntu/nrf5340_psa"
       prj_name: "psa"
       test_input: ["run"]
       successful_output: ["encrypted"]
       boards_to_test_on: ["nrf5340", "mps2-an521" ]

From all this parameters I would like to create a costume ID from prj_name and board_to_test_on .根据所有这些参数,我想从prj_nameboard_to_test_on创建一个服装 ID。 So I would be able to use the -k option to run a specific test.所以我可以使用-k选项来运行特定的测试。

Do you mean custom ID?你是说自定义ID吗?

import yaml
import pytest

with open('test_data.yaml', 'r') as f:
    data = yaml.safe_load(f)

test_data = []
for project in data['Zephyr']['projects']:
    prj_root = project['prj_root']
    prj_name = project['prj_name']
    successful_output = project['successful_output']
    test_input = project['test_input']
    for board_type in project['boards_to_test_on']:
        test_id = f"{prj_name}_{board_type}"
        test_data.append((prj_root, board_type, successful_output, test_input, test_id))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM