pytest结合数据驱动读取json文件
- json是JS对象
 - 全称是JavaScript Object Notation
 - 是一种轻量级的数据交换格式
 - json结构
对象{“key”:value}
数组[value1,value2…] 
{
    "name":"hogwarts",
    "detail":{
        "course":"python",
        "city":"北京"
    },
    "remark":[1000,666,888]
}
json文件使用
- 查看json文件
pycharm
txt记事本 - 读取json文件
内置函数open()
内置库json
方法: json.loads(),解析一个文本对象,并返回成字典格式 
 def get_json():
   with open('demo.json','r') as f:
       data = json.loads(f.read())
       print(data)
方法:json.dumps(), 解析一个字典格式对象,并保存为文本格式
def dump_json():
    """
    json.dumps(), 解析一个字典格式对象,并保存为文本格式(即字符串格式)
    :return:
    """
    with open("data/params.json", "r", encoding="utf-8") as f:
        raw = json.loads(f.read())
        res = json.dumps(raw)
        print(type(res))
        print(res)
        f.close()
    with open("data/params.json", "w", encoding="utf-8") as f:
        f.write(res)
        f.close()
结合数据驱动读取 json 文件
#测试my_add方法的测试用例
import pytest
import json
from func.operation import my_add
# 用到json文件中的数据时,就需要读取出来
def get_json():
    #读取json文件内容
    with open("../data/params.json",'r') as f:
        data = json.loads(f.read()) #将json文件加载成python对象:[[],[],[]]
        return list(data.values())
class TestWithJSON:
    @pytest.mark.parametrize('x,y,expected',get_json())
    def test_add(self, x, y, expected):
        assert my_add(int(x),int(y)) == int(expected)
        