如何测试函数是否正常运行或返回特定值

时间:2013-04-05 03:33:58

标签: python unit-testing

我只需要测试函数是否运行没有失败或 测试返回的值是否为以下值之一:

('NDP', None) or ('CPC', None) or ('GREEN', None) or ('LIBERAL', None)

def test_single_zero_ballot(self):
    self.assertEqual(voting_systems.voting_irv({('NDP','CPC','LIBERAL','GREEN'):0}),
                     (('NDP', None) or ('CPC', None) or ('GREEN', None) or ('LIBERAL', None)),
    'Fails to run when there is a single ballot with zero votes')

1 个答案:

答案 0 :(得分:1)

我建议您使用assertIn,因为您正在检查该值是否是以下几个值之一:

def test_single_zero_ballot(self):
    valid_values = [('NDP', None), ('CPC', None), ('GREEN', None), ('LIBERAL', None)]    
    self.assertIn(voting_systems.voting_irv({('NDP','CPC','LIBERAL','GREEN'):0}),
                     valid_values,
    'Fails to run when there is a single ballot with zero votes')

有关详细信息,请参阅docs