17

I am trying to do a simple test in Python using 'unittest', to see if a class throws an exception if it gets an unsuitable input for the constructor. The class looks like this:

class SummaryFormula:
    def __init__( self, summaryFormula):
        self.atoms = {}
        for atom in re.finditer( "([A-Z][a-z]{0,2})(\d*)", summaryFormula):
            symbol = atom.group(1)
            count = atom.group(2)

            if pocet != "":
                self.atoms[ symbol] = int(count)
            else:
                self.atoms[ symbol] = 1

My test is the following:

    class ConstructorTestCase(unittest.TestCase):
      def testEmptyString(self):
        self.assertRaises(TypeError, ukol1.SummaryFormula(), "testtest")

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

All I want is the test to fail, meaning that the exception of unsuitable input for constructor is not handled.

Instead, I get an error: __init__() takes exactly two arguments (1 given).

What am I missing? What is the second argument I should specify?

Also, what type of Error should I use to handle exception that an input not matchable by my regexp was passed to the constructor?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomas Novotny
  • 7,547
  • 9
  • 26
  • 23

5 Answers5

28

A more Pythonic way is to use with command (added in Python 2.7):

with self.assertRaises(SomeException):
    do_something()

Documentation: assertRaises

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pratyush
  • 5,108
  • 6
  • 41
  • 63
26

assertRaises is a little confusing, because you need to give it the callable, not an expression that makes the call.

Change your code to:

self.assertRaises(TypeError, ukol1.SummaryFormula, "testtest")

In your code, you are invoking the constructor yourself, and it raises an exception about not having enough arguments. Instead, you need to give assertRaises the callable (ukol1.SummaryFormula), and the arguments to call it with ("testtest"). Then it can call it, catching and checking for exceptions.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
13

That’s because your class requires a parameter while instantiating the object.

While you are passing

ukol1.SummaryFormula()

You should have been passing the parameter summaryFormula to it.

ukol1.SummaryFormula(someSummaryFormula)

Also the confusion is because your class name is SummaryFormula and the parameter that you pass to __init__ is also SummaryFormula

Or it should this be

self.assertRaises(TypeError, ukol1.SummaryFormula, "testtest")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • This was of course the first thing that I tried, but if I use self.assertRaises(TypeError, ukol1.SummaryFormula("testtest"), "testtest"), I get other error: SummaryFormula instance gas no __call__ method. And isn't it that you specify the arguments passed to the function tested in assertRaises as a third (or fourth, fifth, ...) argument of the assertRaises() function? – Tomas Novotny Oct 06 '10 at 21:51
  • 1
    @Tomas Novotny : I did add the second change while I was trying to run your code. Repeating it here : self.assertRaises(TypeError, ukol1.SummaryFormula, "testtest") – pyfunc Oct 06 '10 at 21:54
7

Since none of the other answers point on how you can use the context that encapsulates the code that causes the exception, here's how you can do that.

with self.assertRaises(ValueError) as ctx:
    <some code that throws an exception>

expected_msg = 'foo_bar_baz'
self.assertEquals(ctx.exception.message, expected_msg)

Attributes of interest in this unittest.case._AssertRaisesContext, are:

  • exception
  • expected
  • expected_regexp
  • failureException
Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • 1
    ctx.exception does not have attribute 'message' for me, but self.assertEquals(str(ctx.exception), expected_msg) works. – Bryan Prazen May 03 '22 at 21:47
4

A more-generic alternate format is

args=['testtest']
kwargs = {}
self.assertRaises(TypeError, ukol1.SummaryFormula, *args, **kwargs)

This is useful if your constructor is polymorphic and you want to loop over a list of different ways of miswriting the arguments, e.g.:

arg_lists = [
    ['testtest'],
    ['anothertest'],
    ['YAT'],
]
for args in arg_lists:
    self.assertRaises(TypeError, ukol1.SummaryFormula, *args)
Sarah Messer
  • 3,592
  • 1
  • 26
  • 43