views:

102

answers:

4

Hello,

I am runninig test's with Python Unittest. I am running tests but I want to do negative testing and I would like to test if a function throw's an exception, it passes but if no exception is thrown the test fail's. The script I have is:

    try:
        result = self.client.service.GetStreamUri(self.stream, self.token)
        self.assertFalse
    except suds.WebFault, e:                        
        self.assertTrue
    else:
        self.assertTrue

This alway's passes as True even when the function work's perfectly. I have also tried various other way's including:

    try:
        result = self.client.service.GetStreamUri(self.stream, self.token)
        self.assertFalse
    except suds.WebFault, e:                        
        self.assertTrue
    except Exception, e:
        self.assertTrue

Does anyone have any suggestions?

Thanks

I have tried assertRaises with no luck.

    try:
        result = self.client.service.GetStreamUri(self.stream, self.token)
        self.assertRaises(WebFault)
    except suds.WebFault, e:                        
        self.assertFalse
    except Exception, e:                        
        self.assertTrue

It still passes. For some reason it does not try and do the assertRaises statement. I have also tried: (The function should fail but the test should pass)

    try:
        result = self.client.service.GetStreamUri(self.stream, self.token)

    except suds.WebFault, e:                        
        self.assertFalse
    except Exception, e:                        
        self.assertTrue
    else:
        self.assertFalse

For some reason even when the function passes it will not raise an error. It always no matter what goes to Exception. Unless there is an else statement then it goes to that.

Found a way that work's but it seem's a very poor way of doing things:

Can anyone suggest a cleaner way of doing this?

    try:
        result = self.client.service.GetStreamUri(self.stream, self.token)

    except suds.WebFault, e:                        
        self.assertFalse
    except Exception, e:
        pass

    try:
        result==result
    except:
        result=None
    if result==None:
        assert True
    else:
        assert False
+1  A: 

Have you tried assertRaises?

Hank Gay
A: 

You should maybe call self.assertTrue(True) and self.assertFalse(True), because you are not realy calling the functions right now, just referencing them. Still, there has to be a cleaner way to do this.

sharvey
A: 

The excellent nose unit testing provides a @raises decorator:

@raises(TypeError)
def test_raises_type_error():
    raise TypeError("This test passes")
Olivier
+4  A: 

TestCase.assertRaises is what you need however in your example you appear to be misusing it slightly. You need something like:

def test_GetStreamUri(self):
    self.assertRaises(WebFault, self.client.service.GetStreamUri)
    result = self.client.service.GetStreamUri(self.stream, self.token)

You need to tell unittest about the callable the will raise the exception before you invoke the callable.

Tendayi Mawushe