Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python unittest assertRaises

I copied this verbatim from python.org unittest documentation:

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()

But I get this error message from python 2.7.2 [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2:

.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)

How can I get assertRaises() to work properly?

like image 475
Steve Schulist Avatar asked Jul 11 '13 04:07

Steve Schulist


People also ask

What does assertRaises do in Python?

assertRaises() – It allows an exception to be encapsulated, meaning that the test can throw an exception without exiting the execution, as is normally the case for unhandled exceptions. The test passes if exception is raised, gives an error if another exception is raised, or fails if no exception is raised.

How do I raise an exception in Unittest Python?

assertRaises(exception, callable, *args, **kwds) Test that an exception (first argument) is raised when a function is called with any positional or keyword arguments. The test passes if the expected exception is raised, is an error if another exception is raised, or fails if no exception is raised.

How do you check exceptions in Python?

In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause. We can thus choose what operations to perform once we have caught the exception.


1 Answers

If you're using 2.7 and still seeing this issue, it could be because you're not using python's unittest module. Some other modules like twisted provide assertRaises and though they try to maintain compatibility with python's unittest, your particular version of that module may be out of date.

like image 157
Cory Klein Avatar answered Sep 19 '22 13:09

Cory Klein