Testing a Sanic Web API


#1

How does one test a Sanic Web API? The introduction and quick start parts are quite difficult for beginners. I tried the following (stolen from: https://sanic.readthedocs.io/en/latest/sanic/testing.html)

import pytest
from sanic import response, Sanic
from sanic.websocket import WebSocketProtocol


@pytest.yield_fixture
def app():
    app = Sanic("test_sanic_app")

    @app.route("/test_get", methods=['GET'])
    async def test_get(request):
        return response.json({"GET": True})

    @app.route("/test_post", methods=['POST'])
    async def test_post(request):
        return response.json({"POST": True})

    yield app


@pytest.fixture
def test_cli(loop, app, test_client):
    return loop.run_until_complete(test_client(app, protocol=WebSocketProtocol))


#########
# Tests #
#########

async def test_fixture_test_client_get(test_cli):
    """
    GET request
    """
    resp = await test_cli.get('/test_get')
    assert resp.status == 200
    resp_json = await resp.json()
    assert resp_json == {"GET": True}


async def test_fixture_test_client_post(test_cli):
    """
    POST request
    """
    resp = await test_cli.post('/test_post')
    assert resp.status == 200
    resp_json = await resp.json()
    assert resp_json == {"POST": True}

This resulted in the following error:

@pytest.fixture
def test_cli(loop, app, test_client):
E fixture ‘loop’ not found

How I run the tests:
pytest test_example.py

My machine:

  • Windows 10
  • Python 3.6
  • pipenv
  • sanic 18.12.0

Questions

  1. What is this ‘loop’ not found error?
  2. How to test a Sanic web api, if the tests are seperated from the api (in another folder)?

#2

It looks to me like you are trying to use the pytest-sanic code. Make sure in that case you have the plugin installed.

While you certainly can use pytest-sanic, you can also run tests with pytest along without the pytest-sanic plugin. Make sure you also have aiohttp installed since the current test client uses it (this may change in the future).

Here is a pretty barebones setup you can use:

.
├── requirements.txt
├── src
│   ├── __init__.py
│   └── server.py
└── tests
    ├── __init__.py
    └── test_sample.py


# ./src/server.py

from sanic import Sanic, response

app = Sanic(__file__)


@app.route("/", methods=['GET'])
async def test_get(request):
    return response.json({"GET": True})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=1234)



# ./tests/test_sample.py

from src.server import app


def test_get():
    _, response = app.test_client.get("/")
    assert response.status == 200

Then, you should be able to get up and running with:

$ pytest
======================== test session starts =========================
platform linux -- Python 3.7.2, pytest-4.3.0, py-1.8.0, pluggy-0.9.0
rootdir: /tmp/sample, inifile:
collected 1 item                                                     

tests/test_sample.py .                                         [100%]

====================== 1 passed in 0.11 seconds ======================

#3

thank you, works like a charm!