JSON 的单元测试以检查列数

Unit Test for JSON to check number of columns

JSON 文件结构如下:

 "name": "zener diode",
 "components": "4-1/2 designed \n1/2 to reliably allow \n1 current to flow "backwards" ",
 "url": "http://https://en.wikipedia.org/wiki/Zener_diode",
 "image": "http://https://en.wikipedia.org/wiki/Zener_diode.jpg",
 "Thresholdtime": "45M",
 "WaferYield": "8",
 "datePublished": "2010-10-14",
 "downTime": "5M",
 "description": "I have a good, basic wafer "

我正在尝试创建一个测试来验证 JSON 中的列始终具有与上面所示相同的结构和编号,它有 9 列。

如果超过9列,则无效JSON。

如果可以使用 python 或 pytest,请帮助我。

非常感谢

如果你只想检查它的长度:

obj = {
"name": "zener diode",
 "components": "4-1/2 designed \n1/2 to reliably allow \n1 current to flow "backwards" ",
 "url": "http://https://en.wikipedia.org/wiki/Zener_diode",
 "image": "http://https://en.wikipedia.org/wiki/Zener_diode.jpg",
 "Thresholdtime": "45M",
 "WaferYield": "8",
 "datePublished": "2010-10-14",
 "downTime": "5M",
 "description": "I have a good, basic wafer "
}


assert len(obj.keys()) == 9
# true

试试这个:

assert len(my_dict) == 9