这是我的功能:

def get_value(request, param):
  s = get_string(request, param)
  value = re.search('(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)', s)
  if not value:
    print 'match not found!'
    raise Exception('incorrect format: %s' % param)

测试功能:
def test_get_value(self):
    m = test_mocks.HttpRequestMock(REQUEST = {'start_date': '2011.07.31'})
    print '*************************'
    print 'date format changed'
    self.assertRaises(Exception, get_value, (m, 'start_date'))
    print '*********************
get_value不打印:找不到匹配项!

最佳答案

您的python版本似乎有问题。我猜您正在使用2.6版以下的python。
尝试将函数参数作为其他参数传递给函数,即不要将它们放在元组中。试试这个。

self.assertRaises(Exception, helpers.get_value, m, 'start_date')

关于python - python单元测试用例中self.assertRaises未涵盖的异常语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12889366/

10-12 16:52
查看更多