遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

pytest简易教程(15):parametrize参数化数据来自json文件

 

pytest简易教程汇总,详见https://www.cnblogs.com/uncleyong/p/17982846

测试数据

case.json

 

[
    {
        "uname": "ren",
        "pwd": "123"
    },
    {
        "uname": "qzcsbj",
        "pwd": "456"
    }
]

  

parametrize从json获取数据

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : 韧
# @wx :ren168632201
# @Blog :https://www.cnblogs.com/uncleyong/

import pytest
import json
import os

# 获取项目路径
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

def read_data_from_json(file_path):
    data = {}
    with open(file_path, 'r', encoding="utf-8") as fp:
        data = json.load(fp)
    print(f"返回的参数:{data}")
    return data

@pytest.mark.parametrize("param", read_data_from_json(BASE_PATH+"/data/case.json"))
def test_case(param):
    print(f"uname={param['uname']}, pwd={param['pwd']}")

  

结果:

 

posted @ 2024-02-23 21:42  全栈测试笔记  阅读(174)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end