How to test url in Blueprint?
jonahfang opened this issue · comments
How to test url in Blueprint?
You can just test it like normal url, eg.
# module.py
from sanic import Sanic, Blueprint
from sanic.response import json
app = Sanic()
bp = Blueprint("test_blueprints", url_prefix="/blueprint")
@app.route("/")
async def test(request):
return json({"hello": "world"})
@bp.route("/bp")
async def test_blueprints(request):
return json({"foo": "bar"})
app.blueprint(bp)
Then,
# test.py
async def test_sanic_route(test_cli):
resp = await test_cli.get('/')
assert resp.status == 200
assert (await resp.json()) == {"hello": "world"}
async def test_sanic_blueprint(test_cli):
resp = await test_cli.get('/blueprint/bp')
assert resp.status == 200
assert (await resp.json()) == {"foo": "bar"}
of course, you have to define test_cli
fixture.
BTW, Sanic v0.5.4
has several small issues(a little buggy), but have been fixed in master branch. I'd suggest to use master branch. and i'm working on pushing a new release for Sanic.
@yunstanford , thank you very much. It works!