我希望使用py.test进行测试,因为其易用性和自动发现测试的特点。在使用unittest运行测试时,测试运行良好。但是,当我在py.test下运行测试时,出现了“RuntimeError:working outside of application context”错误。
以下是测试代码(test_app.py):
但是,当我使用py.test进行测试时,它会失败:
现在,事实证明,只需做如下操作即可使测试通过:
那么,unittest和py.test在处理模块中的文件方面是否有区别呢?很奇怪它会出现足以使Flask感到不满的故障,因为很明显我正在应用上下文中调用app.test_client().get()。这是预期行为还是我应该对py.test提出错误报告呢?
如果相关的话,我从父目录执行测试的原因是因为我没有将模块添加到site-packages的能力,所以我从安装了Flask、py.test等的父目录启动了所有代码。
编辑:问题已解决。这是一个安装问题。添加pythonpath标记,因为那是解决方案。
以下是测试代码(test_app.py):
import unittest
from app import app
class TestAPILocally(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
def testRoot(self):
retval = self.client.get('/').data
self.assertTrue('v1' in retval)
if __name__ == '__main__':
unittest.main()
以下是我正在测试的文件(app.py):
from flask import Flask
from flask.ext.restful import Api, Resource
class APIListAPI(Resource):
def get(self):
return ['v1']
app = Flask(__name__)
api = Api(app)
api.add_resource(APIListAPI, '/')
如您所见,这与Flask网站上的文档非常相似:测试框架。当我使用unittest运行它时,它确实成功了:
$ python tmp1/test_app.py
.
----------------------------------------------------------------------
Ran 1 test in 0.115s
OK
$
但是,当我使用py.test进行测试时,它会失败:
$ ./py.test tmp1/test_app.py
=================== test session starts =========================
platform sunos5 -- Python 2.7.5 -- py-1.4.22 -- pytest-2.6.0
collected 1 items
tmp1/test_app.py F
========================= FAILURES ==============================
_________________ TestAPILocally.testRoot _______________________
self = <tmp1.test_app.TestAPILocally testMethod=testRoot>
def testRoot(self):
> retval = self.client.get('/').data
tmp1/test_app.py:10:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
werkzeug/test.py:762: in get
return self.open(*args, **kw)
flask/testing.py:108: in open
follow_redirects=follow_redirects)
werkzeug/test.py:736: in open
response = self.run_wsgi_app(environ, buffered=buffered)
werkzeug/test.py:659: in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
werkzeug/test.py:855: in run_wsgi_app
app_iter = app(environ, start_response)
tmp1/flask/app.py:1836: in __call__
return self.wsgi_app(environ, start_response)
tmp1/flask/app.py:1820: in wsgi_app
response = self.make_response(self.handle_exception(e))
flask_restful/__init__.py:256: in error_router
if self._has_fr_route():
flask_restful/__init__.py:237: in _has_fr_route
if self._should_use_fr_error_handler():
flask_restful/__init__.py:218: in _should_use_fr_error_handler
adapter = current_app.create_url_adapter(request)
werkzeug/local.py:338: in __getattr__
return getattr(self._get_current_object(), name)
werkzeug/local.py:297: in _get_current_object
return self.__local()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def _find_app():
top = _app_ctx_stack.top
if top is None:
> raise RuntimeError('working outside of application context')
E RuntimeError: working outside of application context
flask/globals.py:34: RuntimeError
================ 1 failed in 1.02 seconds ======================
现在,事实证明,只需做如下操作即可使测试通过:
$ rm tmp1/__init__.py
通过以下方式再次使其失败:
$ touch tmp1/__init__.py
那么,unittest和py.test在处理模块中的文件方面是否有区别呢?很奇怪它会出现足以使Flask感到不满的故障,因为很明显我正在应用上下文中调用app.test_client().get()。这是预期行为还是我应该对py.test提出错误报告呢?
如果相关的话,我从父目录执行测试的原因是因为我没有将模块添加到site-packages的能力,所以我从安装了Flask、py.test等的父目录启动了所有代码。
编辑:问题已解决。这是一个安装问题。添加pythonpath标记,因为那是解决方案。