»
Jul 13th, 2009,
category:
Python
,
Perm Link...
Why not use assertRaises (unittest.TestCase)
Well, I'm using nose and it doesn't need to use standard python unit test library at all. Let do it using nose.
What is the case
You have following class.
class Transformer(object): def __init__(self, *args, **kwargs): try: self.megatron, self.autobots= args except ValueError: raise TypeError('Transformer takes exactly 2 arguments (%s given) ' % len(args))
You want to test calling value error. Below is test code.
from nose.tools import * from transformers import Transformer class TestTransformer(object): @raises(TypeError) def test_value_error(self): obj = Transformer()
Here we go, easy, short and beautiful.
Note, you can catch multiple exception, like this
@raises(TypeError, ValueError)
Complete list nose.tools