1
我正在使用peewee ORM和sanic(sanic-crud)作为应用程序服务器来构建CRUD REST API。事情工作正常。我也写了几个单元测试的例子。python unitests for sanic app
但是,我正在面临运行unittests的问题。问题在于unittests启动sanic应用服务器并在那里停滞不前。它没有运行unittest的例子。但是当我手动按下Ctrl + C时,sanic服务器终止并且unittests执行开始。所以,这意味着应该有一种方法来启动sanic服务器,并在最后继续进行unittests运行并终止服务器。
有人能请我正确的方式写sanic应用程序的单元测试案例吗?
我也跟着官方文档,但没有运气。 http://sanic.readthedocs.io/en/latest/sanic/testing.html
我尝试以下
from restapi import app # the execution stalled here i guess
import unittest
import asyncio
import aiohttp
class AutoRestTests(unittest.TestCase):
''' Unit testcases for REST APIs '''
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
def test_get_metrics_all(self):
@asyncio.coroutine
def get_all():
res = app.test_client.get('/metrics')
assert res.status == 201
self.loop.run_until_complete(get_all())
从restapi.py
app = Sanic(__name__)
generate_crud(app, [Metrics, ...])
app.run(host='0.0.0.0', port=1337, workers=4, debug=True)
'app.run'应该在'if __name__ =='__main __':'块内调用。 – dirn
@dirn如何将应用程序导入到测试文件中呢? –
导入不会更改。 – dirn