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
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值