简体   繁体   中英

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. I want the parameters to be based on data in a YAML file with all needed information

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 . So I would be able to use the -k option to run a specific test.

Do you mean custom 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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