【问题标题】:python unittest assertRaisespython unittest assertRaises
【发布时间】:2013-07-09 05:49:24
【问题描述】:

我从 python.org unittest 文档中逐字复制了这个:

import random
import unittest

class TestSequenceFunctions(unittest.TestCase):

    def setUp(self):
        self.seq = range(10)

    def test_shuffle(self):
        # make sure the shuffled sequence does not lose any elements
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, range(10))

        # should raise an exception for an immutable sequence
        self.assertRaises(TypeError, random.shuffle, (1,2,3))

    def test_choice(self):
        element = random.choice(self.seq)
        self.assertTrue(element in self.seq)

    def test_sample(self):
        with self.assertRaises(ValueError):
            random.sample(self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assertTrue(element in self.seq)

if __name__ == '__main__':
    unittest.main()

但我从 linux2 上的 python 2.7.2 [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] 收到此错误消息:

.E.
======================================================================
ERROR: test_sample (__main__.TestSequenceFunctions)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tmp.py", line 23, in test_sample
    with self.assertRaises(ValueError):
TypeError: failUnlessRaises() takes at least 3 arguments (2 given)

----------------------------------------------------------------------
Ran 3 tests in 0.001s

FAILED (errors=1)

如何让assertRaises() 正常工作?

【问题讨论】:

标签: python unit-testing python-2.7 testing python-unittest


【解决方案1】:

在 python 2.7 中添加了使用 unittest.TestCase.AssertRaises() 作为上下文管理器的功能。 http://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRaises

【讨论】:

    【解决方案2】:

    检查你是否真的在使用 2.7 python。

    使用pythonbrew 测试:

    $ pythonbrew use 2.7.2
    $ python test.py 
    ...
    ----------------------------------------------------------------------
    Ran 3 tests in 0.000s
    
    OK
    $ pythonbrew use 2.6.5
    $ python test.py
    .E.
    ======================================================================
    ERROR: test_sample (__main__.TestSequenceFunctions)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "test.py", line 23, in test_sample
        with self.assertRaises(ValueError):
    TypeError: failUnlessRaises() takes at least 3 arguments (2 given)
    
    ----------------------------------------------------------------------
    Ran 3 tests in 0.000s
    
    FAILED (errors=1)
    

    【讨论】:

    • “2.7 版更改:添加了使用 assertRaises() 作为上下文管理器的功能。”根据python manual.
    【解决方案3】:

    如果您使用的是 2.7 并且仍然遇到此问题,可能是因为您没有使用 python 的 unittest 模块。 twisted 等其他一些模块提供了assertRaises,尽管它们试图保持与 python 的 unittest 的兼容性,但您的该模块的特定版本可能已经过时了。

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2021-01-30
      • 2010-11-19
      • 2011-04-22
      • 2012-01-30
      • 1970-01-01
      相关资源
      最近更新 更多