测试方法时解包错误

时间:2013-07-01 15:02:20

标签: python python-unittest

我有这个方法:

def split_pointer_part(self, line):
    self.before_at, self.after_at = line.split('@', 1)
    return self.before_at, self.after_at

这是有效的,但当我对它进行测试时:

def test_split_pointer_part(self):
        line = '13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005 @ 13796604 n 0000 + 00603894 a 0401 + 00753137 v 0302 + 01527311 v 0203 + 02361703 v 0101 | an overwhelming number or amount; "a flood of requests"; "a torrent of abuse"'
        result = self.wn.split_pointer_part(line)
        answer = ('13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005',' 13796604 n 0000 + 00603894 a 0401 + 00753137 v 0302 + 01527311 v 0203 + 02361703 v 0101 | an overwhelming number or amount; "a flood of requests"; "a torrent of abuse"')
        self.assertEqual(len(result), 2)
        for r, a in zip(result, answer):
            self.assertEqual(r, a)

这是我得到的错误:

self.before_at, self.after_at = line.split('@', 1)
ValueError: need more than 1 value to unpack

我知道我应该在某个地方使用argv,我不知道如何在这种情况下使用它。

1 个答案:

答案 0 :(得分:1)

因为在第二次拆分后没有任何内容...尝试使用以下内容,以确保after_at如果不存在''而不是提出例外:

before_at, after_at = line.partition('@')[::2]