gpt4 book ai didi

python - 如何对 assertDictNotEqual 进行单元测试?

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:28 24 4
gpt4 key购买 nike

我写了一个 contextlib 来更新环境变量,并为 unittest 写了测试:

previous_env = copy.deepcopy(os.environ)
with MYContext() as my_context:
with self.assertRaises(AssertionError):
self.assertDictEqual(dict(os.environ), dict(previous_env))
self.assertDictEqual(dict(os.environ), dict(previous_env))

但我想知道我是否可以比使用 with self.assertRaises(AssertionError) 断言字典差异更好?

最佳答案

我找到了一个 ticket在 Python bugtracker 中,与 unittest 模块中遗漏的方法有关。以下是讨论中的引述:

Hi,

I am the original reporter of the bug. Please forgive me if this is not the place for discussing the issue.

I've thought about it, and stuff like assertDictNotEqual or assertSequenceNotEqual aren't really necessary - it is much easier (and shorter) to use assertNotEqual, and there's no need for any special formatting needed to say that something is equal when it shouldn't.

This is not the case for assertNotRegexpMatches, though. assertNot(re.match(...)) tells me only that False is not True. I'd like it to say how the text matches the regex (the matching part that is). (Sorry for repeating myself, I try to be clearer this time.)

So, this issue could be renamed to "Implement assertNotRegexpMatches."

而且,就我而言,我不认为 assertDictNotEqual 是否有用,因为只有一种情况,断言可能会失败。你应该只使用 assertNotEqual

关于python - 如何对 assertDictNotEqual 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34872501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com