unittest.assertRaise and keyword arguments?

B

Bo Peng

Dear list,

The syntax for using assertRaise is

assertRaise(exception, function, para1, para2,...)

However, I have a long list of arguments (>20) so I would like to test
some of them using keyword arguments (use default for others). Is there
a way to do this except for manually try...except?

Thanks.
Bo
 
G

Giovanni Bajo

Bo said:
The syntax for using assertRaise is

assertRaise(exception, function, para1, para2,...)

However, I have a long list of arguments (>20) so I would like to test
some of them using keyword arguments (use default for others). Is there
a way to do this except for manually try...except?


You can pass keyword arguments to assertRaises without problems:

self.assertRaises(ValueError, myfunc, arg1,arg2, arg3, arg4, abc=0, foo=1,
bar="hello")

Or you can always do something like:

self.assertRaises(ValueError, lambda: myfunc(arg1,arg2, arg3, arg4, abc=0,
foo=1, bar="hello"))
 
B

Bo Peng

Giovanni said:
You can pass keyword arguments to assertRaises without problems:

self.assertRaises(ValueError, myfunc, arg1,arg2, arg3, arg4, abc=0, foo=1,
bar="hello")

Well, I though abc=0 would be keyword arguments for assertRaisers and
never tried it!
Or you can always do something like:

self.assertRaises(ValueError, lambda: myfunc(arg1,arg2, arg3, arg4, abc=0,
foo=1, bar="hello"))

This is also useful.

I now have nothing to complain against assertTaises. :)

Bo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,271
Messages
2,571,361
Members
48,044
Latest member
elitecorp

Latest Threads

Top