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
相关推荐
奶茶精Gaaa2 天前
pytest
pytest
霍格沃兹测试开发学社测试人社区4 天前
软件测试学习笔记丨Pytest 学习指南
软件测试·笔记·测试开发·学习·pytest
神即道 道法自然 如来6 天前
测试面试题:pytest断言时,数据是符点类型,如何断言?
pytest
high_tea6 天前
pytest - 多线程提速
python·pytest
傻啦嘿哟8 天前
自动化测试框架集成:将Selenium集成到pytest与unittest中
selenium·测试工具·pytest
一名在八月份找工作的测试员9 天前
自动化学习1:pytest自动化框架的基本用法:注意事项/断言assert/测试结果分析
学习·自动化·pytest
什么时候才能变强10 天前
Pytest-如何将allure报告发布至公司内网
linux·服务器·pytest
一名在八月份找工作的测试员10 天前
自动化学习2:pytest的高级用法(mark标记/fixture/hook)
学习·自动化·pytest
测试199811 天前
Python+requests+pytest+allure自动化测试框架
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest