简体   繁体   English

使用Unittest Python进行测试

[英]Testing with Unittest Python

I am runninig test's with Python Unittest. 我正在使用Python Unittest进行runninig测试。 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. 即使函数正常运行,此传递也​​始终为True。 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. 我尝试过assertRaises却没有运气。

    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. 由于某种原因,它不会尝试执行assertRaises语句。 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

TestCase.assertRaises is what you need however in your example you appear to be misusing it slightly. TestCase.assertRaises是您所需要的,但是在您的示例中,您似乎在稍微滥用它。 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. 您需要在调用可调用对象之前告诉unittest有关可调用对象的信息,它将引发异常。

您是否尝试过assertRaises

The excellent nose unit testing provides a @raises decorator : 出色的鼻子单元测试提供了@raises装饰器

@raises(TypeError)
def test_raises_type_error():
    raise TypeError("This test passes")

You should maybe call self.assertTrue(True) and self.assertFalse(True) , because you are not realy calling the functions right now, just referencing them. 您可能应该调用self.assertTrue(True)self.assertFalse(True) ,因为您现在还没有真正调用函数,只是引用了它们。 Still, there has to be a cleaner way to do this. 不过,必须有一种更清洁的方法来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM