如何获取pickle.dumps的内容以进行单元测试?

时间:2015-09-15 16:43:52

标签: python unit-testing

我有这样的功能:

def save_data(self):
    nx.write_gpickle(self.graph, "popitgraph.pickle")
    f = open("node_color.pickle", "w")
    pickle.dump(self.colors, f)
    f.close()
    f = open("node_label.pickle", "w")
    pickle.dump(self.labels, f)
    f.close()

在我的测试中,我做到了这一点:

@patch("popit_to_networkx.nx.write_gpickle")
@patch("pickle.dump")
def test_networkx_save_data(self, mock_dump, mock_write_gpickle):
    # Just assign graph a value to make sure it's passed in
    from mock import mock_open
    m_file = mock_open()
    with patch("__builtin__.open", m_file):
        self.popit2networkx.graph = "hulahoop"
        self.popit2networkx.colors = {'color1', 'color2'}
        self.popit2networkx.labels = {'label1', 'label2'}
        self.popit2networkx.save_data()
        self.assertEqual(mock_write_gpickle.call_args,
                         call('hulahoop', 'popitgraph.pickle'))
        self.assertTrue(m_file.called)
        self.assertEqual(m_file.call_args,
                         call('node_label.pickle', 'w'))

当我试着查看mock_dump.call_args时,我总是收到最新电话的内容,如何获取上一次电话的内容?

1 个答案:

答案 0 :(得分:1)

您应该查看mock_dump.call_args_list property