0

Possible Duplicate:
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.

Community
  • 1
  • 1
JackRobinson
  • 225
  • 2
  • 11
  • 24
  • What result do you want? Do you want to create a function that raises an exception? Just do so. Do you want to catch it? Use a try-except block. Do you want something else? Make yourself more clear. – Bas Wijnen Dec 01 '12 at 09:17
  • Well i have to test all the branches of the function, for example how do i assert or something like that that a client has not got an "identitate" i want to be able to expect that and put it in a test something like assertRises("Nu exista ID!",validate_client) – JackRobinson Dec 01 '12 at 09:20
  • It's still not entirely clear to me. Do you want to test if an object has a callable member? You can check that with `if hasattr (obj, 'membername') and callable (obj.membername)`. You suggest that it is a problem that the function raises an exception. What is the problem about it? – Bas Wijnen Dec 01 '12 at 09:27
  • @BasWijnen: He's talking about unit tests. – Thomas Dec 01 '12 at 09:34
  • I have to write test code for all my functions in the project this is a validate function, i have to test all the possibilities , for example if someone has forgot to enter the ID, but then the function rises an exception, si my question is how to I write a test code that see the exceptions, makes sure is the right exception and then passes the test without rising something ... – JackRobinson Dec 01 '12 at 09:34
  • @JackRobinson: What do you mean by "I'm not able to make it work"? What did you try? What did you expect to happen? What happened instead? Edit the answers into your question. – Thomas Dec 01 '12 at 09:34

1 Answers1

2

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()
Marwan Alsabbagh
  • 25,364
  • 9
  • 55
  • 65