当前位置:   article > 正文

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

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

测试函数中使用如下:

    @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
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/141293
推荐阅读