Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更好的文本差异比较 #15

Open
AzideCupric opened this issue Aug 16, 2023 · 3 comments
Open

更好的文本差异比较 #15

AzideCupric opened this issue Aug 16, 2023 · 3 comments

Comments

@AzideCupric
Copy link

目前的nonebug在测试时遇到和期望不相符的文本时,只会显示这样的格式:

f"Application got api call {api} with data {data} but expected {model.data}"

这经常会造成如下对人眼检查不友好的输出:

Application got api call ... with data {'foo': 1, 'bar': {'hello': 'world'}} but expected {'foo': 1, 'bar': {'hello': 'nonebug'}}

希望可以加入对 期望输出 和 实际输出 的比较,类似这样的格式:

  {
      "foo": 1,
      "bar": {
-         "hello": "world"
?                   ^ ^^^

+         "hello": "nonebot"
?                   ^ ^^^^^

      }
  }

这样的输出可以通过标准库difflib实现,大体类似于:

from difflib import ndiff
from json import dumps

print(
	"\n".join(
		ndiff(
			dumps(data, indent=4).splitlines(),
			dumps(model.data, indent=4).splitlines()
		)
	)
)
@yanyongyu
Copy link
Member

预期结果和实际内容并不一定是可json序列化的,需要使用其他方法来进行diff,可以看看pytest有无相关方法

@AzideCupric
Copy link
Author

预期结果和实际内容并不一定是可json序列化的,需要使用其他方法来进行diff,可以看看pytest有无相关方法

Pytest的 assertion-rewriting 似乎符合要求
可以把现在使用的pytest.fail替换为assert

@yanyongyu
Copy link
Member

因为这一块matcher执行的报错会被捕获,所以并不能使用assert来改写输出

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants