Python 如果可以通过`==`比较dicts,为什么需要assertDictEqual?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34414326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is assertDictEqual needed if dicts can be compared by `==`?
提问by trikoder_beta
To be honest I have always used assertDictEqual
, because sometime when I didn't use it I got information, that equal dicts are not the same.
老实说,我一直使用assertDictEqual
,因为有时当我不使用它时,我会得到信息,即相等的字典是不一样的。
But... I know that dicts can be compared by ==
operator:
但是......我知道可以通过==
运算符比较dicts :
>>> {'a':1, 'b':2, 'c': [1,2]} == {'b':2, 'a':1, 'c': [1,2]}
True
Where I actually may need assertDictEqual
?
我实际上可能需要在哪里assertDictEqual
?
采纳答案by jonrsharpe
Basically, it allows unittest
to give you more information about whythe test failed. Compare these two tests:
基本上,它可以unittest
为您提供有关测试失败原因的更多信息。比较这两个测试:
class DemoTest(unittest.TestCase):
D1 = {'a': 1, 'b': 2, 'c': [1, 2]}
D2 = {'a': 1, 'b': 2, 'c': [1]}
def test_not_so_useful(self):
assert self.D1 == self.D2
def test_useful(self):
self.assertDictEqual(self.D1, self.D2)
And their outputs:
以及他们的输出:
Failure
Traceback (most recent call last):
File "...x.py", line 86, in test_not_so_useful
assert self.D1 == self.D2
AssertionError
vs.
对比
Failure
Traceback (most recent call last):
File "...x.py", line 80, in test_useful
self.assertDictEqual(self.D1, self.D2)
AssertionError: {'a': 1, 'c': [1, 2], 'b': 2} != {'a': 1, 'c': [1], 'b': 2}
- {'a': 1, 'b': 2, 'c': [1, 2]}
? ---
+ {'a': 1, 'b': 2, 'c': [1]}
In the latter, you can see exactly what the difference was, you don't have to work it out yourself. Note that you can just use the standard assertEqual
instead of assertDictEqual
, with the same result; per the docs
在后者中,您可以确切地看到不同之处,您不必自己解决。请注意,您可以只使用标准assertEqual
而不是assertDictEqual
,结果相同;根据文档
...it's usually not necessary to invoke these methods directly.
...通常没有必要直接调用这些方法。
回答by lhk
I assume this is in the context of unit testing. The assertDictEqual
method will not only compare the dict
s and evaluate to True
or False
but can give you additional information, such as the exact differences between the two dict
s.
我认为这是在单元测试的背景下。该assertDictEqual
方法不仅会比较dict
s 并评估为True
or False
,还可以为您提供其他信息,例如两个dict
s之间的确切差异。
Moreover, in a good IDE the unit tests will integrate nicely. You can just add a TestCase
, use assertDictEqual
and the IDE will find and run the test for you. The output is then displayed in an easy to read format. This can save you a lot of boilerplate code.
此外,在一个好的 IDE 中,单元测试将很好地集成。您只需添加一个TestCase
, 使用assertDictEqual
,IDE 就会为您找到并运行测试。然后以易于阅读的格式显示输出。这可以为您节省大量样板代码。
I would be very interested in a case where two equal dict
s are not equal when compared with ==
.
我会对两个相等的dict
s 与==
.
回答by Chris Barker
This is part of a broader question:
这是一个更广泛的问题的一部分:
Why does unittest
have all the special asserts at all?
为什么unittest
有所有的特殊断言呢?
The answer is that the primary job of the UnitTest
assert*
methods is to give you meaningful output when a test fails. Take a look at the unittest
module code -- that really is mostly what they do (only what they do?)
答案是这些方法的主要工作是在UnitTest
assert*
测试失败时为您提供有意义的输出。看看unittest
模块代码——这真的是他们所做的主要事情(只是他们所做的?)
Given that Python is a dynamic language with easy introspection, why bother will all that? And the answer is "because unittest
was ported from the Java junit
package, and that's how they did it in Java" (and probably had to, given how much harder or impossible it is to introspect at run time).
鉴于 Python 是一种易于内省的动态语言,为什么还要麻烦呢?答案是“因为unittest
是从 Javajunit
包中移植过来的,这就是他们在 Java 中的做法”(并且可能不得不这样做,因为在运行时内省是多么困难或不可能)。
So my recommendation: unless you are writing tests for the standard library, don't use unittest at all -- all it does is get in the way. I use pytest
. nose
may be a good option as well. It make it quicker and easier to write tests, and you get excellent reporting when you get errors.
所以我的建议是:除非您正在为标准库编写测试,否则根本不要使用 unittest —— 它所做的只是妨碍。我用pytest
. nose
也可能是一个不错的选择。它使编写测试变得更快、更容易,并且在出现错误时可以获得出色的报告。
It also includes lots of nifty features for parameterized testing, fixtures, test configuration, mocking, etc...
它还包括许多用于参数化测试、夹具、测试配置、模拟等的漂亮功能......
If you are on a project that already uses unittest
-- you still can run your tests with pytest
, and get many of its advantages.
如果您在一个已经使用的项目中unittest
——您仍然可以使用 运行您的测试pytest
,并获得它的许多优点。