yunstanford / pytest-sanic

a Pytest Plugin for Sanic.

Home Page:http://pytest-sanic.readthedocs.io/en/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to test url in Blueprint?

jonahfang opened this issue · comments

How to test url in Blueprint?
commented

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"}
commented

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!