python– 单元测试(烧瓶 – 静止)GET API调用时获得500内部服务器错误

python– 单元测试(烧瓶 – 静止)GET API调用时获得500内部服务器错误,第1张

概述我已经从我的烧瓶式API模块中单独测试了所有方法.现在我想通过实际进行API调用来测试get方法.我期待这个测试的错误400.我的资源类class Response(Resource): @marshal_with(response_params_get_responses_on_job) def get(self, filter_name

我已经从我的烧瓶式API模块中单独测试了所有方法.现在我想通过实际进行API调用来测试get方法.我期待这个测试的错误400.

我的资源类

class Response(Resource):    @marshal_with(response_params_get_responses_on_job)    def get(self,filter_name=None):        try:            response = self.process_get_request(filter_name)            if not response['users']:                raise MyValIDationError("No data found")            return response        except MyValIDationError as err:            abort(404,message=err)        except ValueError as mistake:            abort(400,message=mistake)

我的单位测试

# Todo - Failing!@mock.patch('application.resources.response.Response.process_get_request',autospec=True)def test_get_400(self,process_get_request_mock):    process_get_request_mock.sIDe_effect = ValueError("some error")    app = Flask(__name__)    app.deBUG = True    API = API(app,prefix='/API/v2')    API.add_resource(Response,'/user/responses',endpoint='job_responses')    API.init_app(app)    with app.test_clIEnt() as clIEnt:        resp = clIEnt.get('/API/v2/user/responses',environ_base={'http_USER_AGENT': 'Chrome'},headers={'Content-type': 'application/Json'})        self.assertEqual(resp.status_code,400)

我的测试失败,因为我得到的响应是错误500

AssertionError: 500 != 400

堆栈跟踪

FailureTraceback (most recent call last):file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/mock.py",line 1201,in patchedreturn func(*args,**keywargs)file "/home/hussain/workspace/my-app/tests/unittests/test_Response.py",line 38,in test_get_400headers={'Content-type': 'application/Json'})file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/werkzeug/test.py",line 774,in getreturn self.open(*args,**kw)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/testing.py",line 108,in openfollow_redirects=follow_redirects)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/werkzeug/test.py",line 742,in openresponse = self.run_wsgi_app(environ,buffered=buffered)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/werkzeug/test.py",line 659,in run_wsgi_apprv = run_wsgi_app(self.application,environ,line 867,in run_wsgi_appapp_rv = app(environ,start_response)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py",line 1836,in __call__return self.wsgi_app(environ,line 1820,in wsgi_appresponse = self.make_response(self.handle_exception(e))file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_cors/extension.py",line 110,in wrapped_functionreturn cors_after_request(app.make_response(f(*args,**kwargs)))file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py",line 270,in error_routerreturn original_handler(e)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py",in error_routerreturn original_handler(e)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py",line 1403,in handle_exceptionreraise(exc_type,exc_value,tb)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py",line 267,in error_routerreturn self.handle_error(e)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_restful/__init__.py",in error_routerreturn self.handle_error(e)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py",line 1817,in wsgi_appresponse = self.full_dispatch_request()file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask/app.py",line 1477,in full_dispatch_requestrv = self.handle_user_exception(e)file "/home/hussain/workspace/venv/local/lib/python2.7/site-packages/flask_cors/extension.py",line 1363,in handle_user_exceptionassert exc_value is eAssertionError

通过在get方法中放置一个调试点,我看到执行一直持续到中止(400,消息=错误).

然后发生了什么?为什么我的测试失败了?

最佳答案我一直认为问题出在我的单元测试中.我错了!

问题出在我的代码中.正如米歇尔所说,我的测试已经找到了.

中止方法中的消息kwarg应该是一个字符串,而是我将它传递给异常对象.所以我在中止线上得到了一个例外.

所以我纠正了它

abort(400,message=mistake.message)

或者我也可以写

abort(400,message=str(mistake))

现在我的考试正在通过. 总结

以上是内存溢出为你收集整理的python – 单元测试(烧瓶 – 静止)GET API调用时获得500内部服务器错误全部内容,希望文章能够帮你解决python – 单元测试(烧瓶 – 静止)GET API调用时获得500内部服务器错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1207694.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存