python - 抛出显示差异的异常
问题描述
我正在创建一个在生成输出文件的一些测试之后执行的函数。我想检查进程在内存中的输出与之前的测试运行是否存在差异,如果有任何差异,则显示测试日志中的差异并且测试失败。
with open(os.path.join(path, f"{test_name}.json"), "r") as local_file:
local_data = json.loads(local_file.read())
differences = jsondiff.diff(local_data, payload["args"][1])
if differences:
print(differences)
raise ValueError("There are some differences with {test_name}.json file")
但我不想展示这一点,我想展示差异和 test_name。
我是否需要在此方法中添加断言或创建客户异常?
解决方案
你希望你的字符串是一个f 字符串
with open(os.path.join(path, f"{test_name}.json"), "r") as local_file:
local_data = json.loads(local_file.read())
differences = jsondiff.diff(local_data, payload["args"][1])
if differences:
print(differences)
raise ValueError(f"There are some differences with {test_name}.json file")
#This ^
#Is what you were missing
推荐阅读
- sql - 雪花无法插入多行使用插入具有 md5 字段
- c# - NLog 没有将调试事件写入日志
- http - 无法在中间件中跟踪 HTTP 响应代码
- javascript - 我如何在 redux-saga 1 秒后调度?
- discord - 如何使用 Discord.js 获取谁在观看流媒体
- python - 有人可以解释这个列表理解的功能吗?
- android - Android - 房间迁移没有正确处理 CREATE 表
- react-native - 更改 FlatList 滚动条的颜色 react native
- linux - Windows 子系统 Linux sed 突击队无法通过 bat 文件工作
- macos - 如何在 mac 中通过 cli 设置默认钥匙串?