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']}")

结果:

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐