pytest.param作为pytest.mark.parametrize的参数进行调用

pytest.param:在 pytest.mark.parametrize 中可以作为一个指定的参数进行调用
获取数据库(网页端)数据,通过pytest.param包装成数据包用于pytest.mark.parametrize 中实现数据驱动调用。

import os
import pytest
import json
from json import decoder
# from common.basic_module.Excel import ExcelReader
import requests
from run import Test_Plan_Url

TOPDIR = os.path.dirname(__file__)

# parse excel data: from flow or pytest
def generate_data(flow="", excel_path="Not Using", excel_name="Not Using"):
    print("\nflow id is: {}".format(flow))
    test_data = []
    if flow:
        response = requests.get(Test_Plan_Url + "=" + flow)
        if response.json()['success']:
            cases = response.json()['result']['cases']
            print(f"case num :{len(cases)}")
            for i in range (len(cases)):
                case = cases[i]
                id_flag = str(case["id"])                  
                case_title = case["caseName"]
                try:
                    demo_info = case["filtersConditionDOMap"]["demo_info"][0]["conditionContent"]
                except:
                    demo_info = None

                # 默认run字段无内容表示需要运行
                if run == '' or run == None:
                    test_data.append(pytest.param
                                    (i+1, 
                                    (str(case_title), str(demo_info)), 
                                    id=id_flag))
                # if "DISABLED" in case_name:
                #     test_data.append([i+1, pytest.param((str(case_type), str(case_name), run_case), marks=pytest.mark.xfail), id_flag])
                # else:
                #     test_data.append([i+1, (str(case_title), str(cmd), passexp, failexp, str(priority)), id_flag])

    else:
        print(f"\nexcel_path is {excel_path}, excel_name is {excel_name}\n")
        id_flag = None
        reader = ExcelReader(excel_path)
        reader.set_sheet(excel_name)
        start_line = 2
        for i in range(start_line,reader.rows):
            row_list = []
            for row in reader.sheet[i]:
                row_list.append(row.value)
            test_data.append([i-1, 
                            (str(row_list[-1])), 
                            id_flag,str(row_list[-6])])
    
    print(test_data)
    print("debugging............\n")
    print(type(test_data))
    # execlcase顺序(导入顺序)和flow中id是对应的,但是flow传过来的case无序,需要按照case_id排序.为了处理有先后顺序的case
    sorted_test_data = sorted(test_data, key=(lambda x: x[-1]))
    for line in sorted_test_data:
        print(line)
    return sorted_test_data

测试函数中使用如下:

    @pytest.mark.parametrize("num_id, case_info", generate_data(FLOW_TEST_ID))
    def test_qemu(self, num_id, case_info):
        demo_info = case_info[1]
        pass

你可能感兴趣的:(pytest)