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

pytest.param:在 pytest.mark.parametrize 中可以作为一个指定的参数进行调用

获取数据库(网页端)数据,通过pytest.param包装成数据包用于pytest.mark.parametrize 中实现数据驱动调用。

python 复制代码
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

测试函数中使用如下:

python 复制代码
    @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
相关推荐
头疼的程序员17 小时前
allure生成测试报告(搭配Pytest、allure-pytest)
测试工具·pytest
文人sec1 天前
接口自动化测试设计思路--设计实战
python·https·单元测试·自动化·pytest
测试开发Kevin3 天前
从投入产出、效率、上手难易度等角度综合对比 pytest 和 unittest 框架
python·pytest
测试开发Kevin5 天前
以pytest_addoption 为例,讲解pytest框架中钩子函数的应用
python·pytest
川石教育11 天前
Pytest中的fixture装饰器详解
python自动化测试·pytest·pytest自动化测试框架·pytest测试框架·pytest单元测试框架
春风又。11 天前
接口自动化——参数化
python·测试工具·自动化·pytest
XTY0013 天前
mac电脑pytest生成测试报告
pytest
程序员的世界你不懂13 天前
pytest-前后置及fixture运用
pytest
天才测试猿15 天前
基于Pytest接口自动化的requests模块项目实战以及接口关联方法详解
自动化测试·软件测试·python·测试工具·单元测试·测试用例·pytest
HtwHUAT15 天前
五、UI自动化测试05--PyTest框架
经验分享·python·ui·pytest