python sanic 应用程序的统一测试
python unitests for sanic app
我正在使用 peewee ORM 和 sanic(sanic-crud) 作为应用服务器构建 CRUD REST API。一切正常。我为此写了几个单元测试用例。
但是,我在单元测试中遇到了问题 运行。问题是 unittests 启动了 sanic app server 并停在那里。它根本不是 运行ning 单元测试用例。但是当我手动按下 Ctrl+C 时,sanic 服务器被终止并且单元测试开始执行。因此,这意味着应该有一种方法可以启动 sanic 服务器并继续单元测试 运行 并在最后终止服务器。
有人可以告诉我为 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 语句移动到主块
最终设法 运行 单元测试
# tiny app server starts here
app = Sanic(__name__)
generate_crud(app, [Metrics, ...])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=1337, debug=True)
# workers=4, log_config=LOGGING)
和
from restapi import app
import json
import unittest
class AutoRestTests(unittest.TestCase):
''' Unit testcases for REST APIs '''
def test_get_metrics_all(self):
request, response = app.test_client.get('/metrics')
self.assertEqual(response.status, 200)
data = json.loads(response.text)
self.assertEqual(data['metric_name'], 'vCPU')
if __name__ == '__main__':
unittest.main()
我正在使用 peewee ORM 和 sanic(sanic-crud) 作为应用服务器构建 CRUD REST API。一切正常。我为此写了几个单元测试用例。
但是,我在单元测试中遇到了问题 运行。问题是 unittests 启动了 sanic app server 并停在那里。它根本不是 运行ning 单元测试用例。但是当我手动按下 Ctrl+C 时,sanic 服务器被终止并且单元测试开始执行。因此,这意味着应该有一种方法可以启动 sanic 服务器并继续单元测试 运行 并在最后终止服务器。
有人可以告诉我为 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 语句移动到主块
最终设法 运行 单元测试# tiny app server starts here
app = Sanic(__name__)
generate_crud(app, [Metrics, ...])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=1337, debug=True)
# workers=4, log_config=LOGGING)
和
from restapi import app
import json
import unittest
class AutoRestTests(unittest.TestCase):
''' Unit testcases for REST APIs '''
def test_get_metrics_all(self):
request, response = app.test_client.get('/metrics')
self.assertEqual(response.status, 200)
data = json.loads(response.text)
self.assertEqual(data['metric_name'], 'vCPU')
if __name__ == '__main__':
unittest.main()