示例#1
0
async def test_test_app() -> None:
    startup = False
    shutdown = False

    app = Quart(__name__)

    @app.before_serving
    async def before() -> None:
        nonlocal startup
        startup = True

    @app.after_serving
    async def after() -> None:
        nonlocal shutdown
        shutdown = True

    @app.route("/")
    async def index() -> str:
        return ""

    async with app.test_app() as test_app:
        assert startup
        test_client = test_app.test_client()
        await test_client.get("/")
        assert not shutdown
    assert shutdown
示例#2
0
文件: test_app.py 项目: pgjones/quart
async def test_test_app() -> None:
    startup = False
    shutdown = False
    serving = []

    app = Quart(__name__)

    @app.before_serving
    async def before() -> None:
        nonlocal startup
        startup = True

    @app.after_serving
    async def after() -> None:
        nonlocal shutdown
        shutdown = True

    @app.while_serving
    async def lifespan() -> AsyncGenerator[None, None]:
        nonlocal serving
        serving.append(1)
        yield
        serving.append(2)

    @app.route("/")
    async def index() -> str:
        return ""

    async with app.test_app() as test_app:
        assert startup
        test_client = test_app.test_client()
        await test_client.get("/")
        assert not shutdown
        assert serving == [1]
    assert shutdown
    assert serving == [1, 2]