解决allure源码报错

一、背景

python自动化用nose框架中allure生成报告,但是case中有异常抛出会报错(错误信息稍后给出),

python 3.5版本

nose配置文件:

with-allure=1
logdir=results


二、执行的错误信息:

Traceback (most recent call last):
  File "/home/python/lib/python3.6/site-packages/nose/case.py", line 134, in run
    self.runTest(result)
  File "/home/python/lib/python3.6/site-packages/nose/case.py", line 152, in runTest
    test(result)
  File "/home/python/lib/python3.6/unittest/case.py", line 649, in __call__
    return self.run(*args, **kwds)
  File "/home/python/lib/python3.6/unittest/case.py", line 609, in run
    self._feedErrorsToResult(result, outcome.errors)
  File "/home/python/lib/python3.6/unittest/case.py", line 537, in _feedErrorsToResult
    result.addFailure(test, exc_info)
  File "/home//python/lib/python3.6/site-packages/nose/proxy.py", line 146, in addFailure
    plugins.addFailure(self.test, err)
  File "/home/python/lib/python3.6/site-packages/nose/plugins/manager.py", line 99, in __call__
    return self.call(*arg, **kw)
  File "/home/python/lib/python3.6/site-packages/nose/plugins/manager.py", line 167, in simple
    result = meth(*arg, **kw)
  File "/home/python/lib/python3.6/site-packages/nose_allure/__init__.py", line 26, in wrapper
    return func(self, *args, **kwargs)
  File "/home/python/lib/python3.6/site-packages/nose_allure/__init__.py", line 134, in addFailure
    message, trace = self._parse_tb(err)
  File "/home/python/lib/python3.6/site-packages/nose_allure/__init__.py", line 151, in _parse_tb
    traceback.format_exception_only(trace[0], trace[1])).strip()
  File "/home/python/lib/python3.6/traceback.py", line 136, in format_exception_only
    return list(TracebackException(etype, value, None).format_exception_only())
  File "/home/python/lib/python3.6/traceback.py", line 465, in __init__
    if (exc_value and exc_value.__cause__ is not None
AttributeError: 'str' object has no attribute '__cause__'


三、如何修改:

找到源码位置:

修改_parse_tb方法:注释的原来的,下面是修改后的:

  @staticmethod
    def _parse_tb(trace):
        # message = ''.join(
        #     traceback.format_exception_only(trace[0], trace[1])).strip()
        # trace = ''.join(traceback.format_exception(*trace)).strip()
        # return message, trace
        exc_type, exc_val, tb = trace
        message = exc_message(trace)
        trace = ''.join(traceback.format_exception(
            exc_type,
            exc_val if isinstance(exc_val, exc_type) else exc_type(exc_val),
            tb
        ))
        return message, trace

def exc_message(exc_info):
    """Return the exception's message."""
    exc = exc_info[1]
    if exc is None:
        # str exception
        result = exc_info[0]
    else:
        try:
            result = str(exc)
        except UnicodeEncodeError:
            try:
                result = unicode(exc)  # flake8: noqa
            except UnicodeError:
                # Fallback to args as neither str nor
                # unicode(Exception(u'\xe6')) work in Python < 2.6
                result = exc.args[0]
    return result


展开阅读全文

没有更多推荐了,返回首页