Flask应用程序单元测试中的WSGI StopIteration错误

时间:2019-07-09 14:06:38

标签: python unit-testing flask wsgi

我在ApplicationController.py中定义了以下内容,这是多线程Flask应用程序的路由:

@app.route('/enqueued/')
def enqueuedRoute():

  #Put Url In Queue then Await to Execute Next Job
  putUrlInQueue(request)
  resp = awaitAndExecuteJob()

  return resp.raw.read()

然后我尝试创建以下TestApplicationController.py文件:

import unittest
from unittest.mock import MagicMock
from mock import patch
from controller.ApplicationController import app

class TestApplicationController(unittest.TestCase):

  def setUp(self):
    app.config['TESTING'] = True
    app.config['WTF_CSRF_ENABLED'] = False
    app.config['DEBUG'] = False

    self.app = app.test_client()

  @patch('controller.ApplicationController.request')
  @patch('controller.ApplicationController.awaitAndExecuteJob')
  @patch('controller.ApplicationController.putUrlInQueue')
  def test_enqueuedRoute(self, putUrlInQueue, awaitAndEx, request):
    resp = MagicMock()
    respRaw = MagicMock()
    resp.raw.read.return_value = respRaw
    awaitAndEx.return_value = resp

    ret = self.app.get('/enqueued/')

    assert ret == respRaw

这样做时,我得到以下错误输出:

Traceback (most recent call last):
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\mock\mock.py", line 1305, in patched
return func(*args, **keywargs)
  File "C:\apps\GBI-ApplicationRequestHandler\test\unit\controller\TestApplicationController.py", line 89, in test_enqueuedRoute
ret = self.app.get('/enqueued/')
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\test.py", line 778, in get
return self.open(*args, **kw)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\flask\testing.py", line 113, in open
follow_redirects=follow_redirects)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\test.py", line 751, in open
response = self.run_wsgi_app(environ, buffered=buffered)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\test.py", line 668, in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\test.py", line 871, in run_wsgi_app
app_rv = app(environ, start_response)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\flask\app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\flask\app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\flask\app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\flask\app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\flask\app.py", line 1642, in full_dispatch_request
response = self.make_response(rv)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\flask\app.py", line 1746, in make_response
rv = self.response_class.force_type(rv, request.environ)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\wrappers.py", line 847, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\wrappers.py", line 57, in _run_wsgi_app
return _run_wsgi_app(*args)
  File "C:\Users\developer\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\test.py", line 890, in run_wsgi_app
  buffer.append(next(app_iter))
StopIteration

我知道该问题专门与return resp.raw.read()行有关,因为如果将其更改为return jsonify(success=True),则测试运行良好。

我该怎么做才能正确模拟响应?

我认为该问题与以下GitHub讨论有关:Link

0 个答案:

没有答案