Prevent custom assert from showing in traceback of python unittest

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 12:14:48
Yoav Glazner

unittest doesn't print tracebacks from frames that have __unittest=True in their globals.

From unittest.result:

def _is_relevant_tb_level(self, tb):
    return '__unittest' in tb.tb_frame.f_globals

So, if you make a helper module, you can emulate that behavior:

helper.py:

__unittest = True
def assert_stuff(s):
    assert s == 'something', "%s is not something" % s

Now you can call this helper from your test case.

I guess that one could make a very neat decorator that make such magic more automatic without a helper module but IMHO you shouldn't make effort to reduce the traceback anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!