Testing a Sanic Web API

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)?

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 ======================

thank you, works like a charm!

1 Like

I am using the above snippet, but unfortunately i am getting the following error:

========================================================= ERRORS =========================================================
___________________________________________ ERROR collecting tests/test_api.py ___________________________________________
ImportError while importing test module ‘/home/smahi/Gits/onm/tests/test_api.py’.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_api.py:1: in
from src.server import app
src/server.py:1: in
from sanic import Sanic, response
E ModuleNotFoundError: No module named ‘sanic’
!!! Interrupted: 1 errors during collection !!!
================================================ 1 error in 7.53 seconds =================================================

Looks like sanic is not installed in your development environment.

With the above example, put the following into a requirements.txt

pytest
sanic

And then run pip install -r requirements.txt.

Let me know how that works for you.

@ahopkins Sanic is installed, because when testing the server.py by issuing

$ python src/server.py

The server launchs properly and the json content is displayed on the browser

Here the output of pip freeze

(venv) ╭─ smahi@smahi-Inspiron-3537 ~/Gits/onm  ‹master*› 
╰─$ pip freeze
aiofiles==0.4.0
aiohttp==3.5.4
async-generator==1.10
async-timeout==3.0.1
atomicwrites==1.3.0
attrs==19.1.0
beautifulsoup4==4.7.1
certifi==2019.6.16
chardet==3.0.4
coverage==4.5.3
gunicorn==19.9.0
h11==0.8.1
h2==3.1.0
hpack==3.0.0
httpcore==0.3.0
httptools==0.0.13
hyperframe==5.2.0
idna==2.8
importlib-metadata==0.18
more-itertools==7.1.0
multidict==4.5.2
packaging==19.0
pkg-resources==0.0.0
pluggy==0.12.0
py==1.8.0
py-cpuinfo==5.0.0
pyparsing==2.4.0
pytest==4.1.0
pytest-benchmark==3.2.2
pytest-cov==2.7.1
pytest-sanic==1.0.0
pytest-sugar==0.9.2
requests==2.22.0
requests-async==0.5.0
rfc3986==1.3.2
sanic==19.6.0
six==1.12.0
soupsieve==1.9.2
termcolor==1.1.0
ujson==1.35
urllib3==1.25.3
uvloop==0.12.2
wcwidth==0.1.7
websockets==6.0
yarl==1.3.0
zipp==0.5.1
(venv) ╭─ smahi@smahi-Inspiron-3537 ~/Gits/onm  ‹master*› 
╰─$
(venv) ╭─ smahi@smahi-Inspiron-3537 ~/Gits/onm  ‹master*› 
╰─$ python --version
Python 3.7.3
(venv) ╭─ smahi@smahi-Inspiron-3537 ~/Gits/onm  ‹master*› 
╰─$

what is the output of

which pytest

I am using pytest (not the plugin)

Right, but I was curious as to what the path of your pytest is. I side your virtual env?

There is something about your environment/tooling that is setup differently Than the example.Im just trying to figure out where. It seems your pytest that is being invoked is not inside the venv. Perhaps it is a system wide installation that is running? in which case sanic is not in that env.

1 Like

Then how can i invoke the inside one ?
It is true that I have pytest installed globally

Your are right,
By invoking local pytest the testing passes.

$ venv/bin/pytest

Thank you so much, sir.

1 Like