简体   繁体   English

单元测试框架在Python中引发了什么异常?

[英]What exception does the unittest framework raise in Python?

I have a python script (main.py) that calls a method in another python script (tester.py) that performs a bunch of tests, using unittest framework, and returns. 我有一个python脚本(main.py),该脚本在另一个python脚本(tester.py)中调用一个方法,该脚本使用unittest框架执行大量测试,然后返回。 What exception does main.py look for in it's try, except block? 除了块,main.py在尝试中查找什么异常?

I've tried the following but the exception is never triggered. 我尝试了以下操作,但从未触发过异常。

try:
  tester.run()
except Exception, ex:
  print ex

According to the documentation it raises AssertionError. 根据文档,它将引发AssertionError。 However, I've tried AssertionError, Exception, but neither work. 但是,我已经尝试了AssertionError,Exception,但是都没有用。

Any ideas. 有任何想法吗。

Depends on which exception is raised. 取决于引发哪个异常。

Method raising exception: 方法引发异常:

def some(self):
    try:
        msg = "My Exception"
        raise HTTPNotFound(msg)
    except Exception as e:
         raise e

Method catching exception 方法捕获异常

try:
    some()
except HTTPNotFound as e:
    print "Not found exception"
except Exception as e:
    print "Generic Exception"

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

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