How to test functions that throw exceptions
Question: > **Possible Duplicate:**  
>  [How do you test that a Python function throws an
> exception?](http://stackoverflow.com/questions/129507/how-do-you-test-that-
> a-python-function-throws-an-exception)

I have to do white-box and black-box testing so i`m wondering how it is
possible to test a function that trows an exceptions, like this one

    
    
    class validator_client():
    
        def validate_client(self,client):
            erori=[]
            if client.get_identitate()=="":
                erori.append("Nu exista ID!")
            if client.get_nume()=="":
                erori.append("Nu exista nume!")
            if (client.get_filme_inchiriate()!="da" ) and (client.get_filme_inchiriate()!="nu") :
                erori.append("Campul 'Filme inchiriate' completat gresit!")
            if len(erori)>0:
                raise ValidatorException(erori)
    

I`ve read something about assertRises() but i can not import the module with
this method, found this on stackowerflow:

    
    
    from testcase import TestCase
    
    import mymod
    
    class MyTestCase(TestCase):
        def test1(self):
            self.assertRaisesWithMessage(SomeCoolException,
                                         'expected message',
                                          mymod.myfunc)
    

but I`m not able to make it work.


Answer: This is a working example that is a simplified version of what you will find
in the python docs. It checks that `random.shuffle` raises a `TypeError`
exception.

    
    
    import random, unittest
    
    class TestSequenceFunctions(unittest.TestCase):
        def test_shuffle(self):
            self.assertRaises(TypeError, random.shuffle, (1,2,3))
    
    unittest.main()
    

