HttpRunnerManager运行用例之后,页面报错

1. 现象

Exception Type: AttributeError
Exception Value: can’t set attribute
Django Version: 2.0.3
Python Version: 3.6.3
Traceback:

Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'ApiManager',
 'djcelery']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 35. response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 128. response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/Cellar/HttpRunnerManager/HttpRunnerManager/activator.py" in process 15. result = fun(request, index) if index else fun(request)
File "/usr/local/Cellar/HttpRunnerManager/ApiManager/views.py" in wrapper 39. return func(request, *args, **kwargs)
File "/usr/local/Cellar/HttpRunnerManager/ApiManager/views.py" in run_test 238. runner.summary = timestamp_to_datetime(runner.summary, type=False)
Exception Type: AttributeError at /api/run_test/
Exception Value: can't set attribute

2. 原因

打开HttpRunnerManager项目,找到文件HttpRunnerManager/ApiManager/views.py
存在报错
HttpRunnerManager运行用例之后,页面报错_第1张图片

3.溯源

可以看到HttpRunnerManager django project下引用的python包
路径为/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/httprunner
文件api.py中定义了一个属性_summary和一个方法summary
HttpRunnerManager运行用例之后,页面报错_第2张图片
报错的语句

runner.summary = timestamp_to_datetime(runner.summary, type=False)

timestamp_to_datetime方法定义在HttpRunnerManager/ApiManager/utils/common.py中

def timestamp_to_datetime(summary, type=True):
    if not type:
        time_stamp = int(summary["time"]["start_at"])
        summary['time']['start_datetime'] = datetime.datetime. \
            fromtimestamp(time_stamp).strftime('%Y-%m-%d %H:%M:%S')

    for detail in summary['details']:
        try:
            time_stamp = int(detail['time']['start_at'])
            detail['time']['start_at'] = datetime.datetime.fromtimestamp(time_stamp).strftime('%Y-%m-%d %H:%M:%S')
        except Exception:
            pass

        for record in detail['records']:
            try:
                time_stamp = int(record['meta_data']['request']['start_timestamp'])
                record['meta_data']['request']['start_timestamp'] = \
                    datetime.datetime.fromtimestamp(time_stamp).strftime('%Y-%m-%d %H:%M:%S')
            except Exception:
                pass
    return summary

该句语句是先调用summary方法获取属性_summary
然后使用timestamp_to_datetime方法将_summary中的时间格式化
最后再赋给属性_summary

4. 解决

runner.summary = timestamp_to_datetime(runner.summary, type=False)

        return render_to_response('report_template.html', runner.summary)

改为

runner._summary = timestamp_to_datetime(runner.summary, type=False)

        return render_to_response('report_template.html', runner._summary)

你可能感兴趣的:(HttpRunnerManager运行用例之后,页面报错)