本文介绍了Python unittest - 与 assertRaises 相对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个测试来确定在给定情况下不会引发异常.

I want to write a test to establish that an Exception is not raised in a given circumstance.

测试是否引发异常很简单...

It's straightforward to test if an Exception is raised ...

sInvalidPath=AlwaysSuppliesAnInvalidPath()
self.assertRaises(PathIsNotAValidOne, MyObject, sInvalidPath) 

...但是你怎么能做到相反的.

... but how can you do the opposite.

我所追求的就是这样的东西......

Something like this i what I'm after ...

sValidPath=AlwaysSuppliesAValidPath()
self.assertNotRaises(PathIsNotAValidOne, MyObject, sValidPath) 

推荐答案

def run_test(self):
    try:
        myFunc()
    except ExceptionType:
        self.fail("myFunc() raised ExceptionType unexpectedly!")

这篇关于Python unittest - 与 assertRaises 相对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 00:07