Python unittest断言复杂字典

时间:2018-06-11 12:08:03

标签: python numpy

我试图执行以下因某些原因失败的断言:

{'a': [('Power',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 2.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)]),
       ('Speed',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 3.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)])],
 'b': [('Power',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 4.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)]),
       ('Speed',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 5.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)])],
 'c': [('Power',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 6.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)]),
       ('Speed',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 7.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)])]} != {'a': [('Power',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 2.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)]),
       ('Speed',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 3.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)])],
 'b': [('Power',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 4.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)]),
       ('Speed',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 5.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)])],
 'c': [('Power',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 6.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)]),
       ('Speed',
        [(datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), 7.0),
         (datetime.datetime(2018, 6, 11, 14, 1, 26, 911251), nan)])]}

我已经尝试了self.assertEqualself.assertDictEqual,但仍然会发生同样的事情。我在测试中创建了一次datetime,并且在所有示例中都是相同的。为什么这会失败?我是Python新手测试的新手,所以一些帮助将不胜感激。

这是我运行assertDictEqual时得到的结果:

AssertionError: {'a':[1093 chars], (datetime.datetime(2018, 6, 11, 14, 30, 14, 965541), nan)])]} != {'a':[1093 chars], (datetime.datetime(2018, 6, 11, 14, 30, 14, 965541), nan)])]}

1 个答案:

答案 0 :(得分:1)

这似乎是问题

assert np.NaN == np.NaN

这失败了。有关here的更多信息。

相反,您可以使用numpy的方法进行断言:

from numpy.testing import assert_equal
assert_equal(instances, expected_instances)

像魅力一样。