Sanic Endpoint Testing using PyTest

Recently, I started working on my very first Python web framework at work called Sanic. While the framework is relatively easy to use, I could not say the same for unit testing as its documentation isn’t very clear to me.

If you’re having issues getting unit testing setup for this web framework, you have come to the right place.

Before We Begin

I want to put a disclaimer that I am using Sanic version 19.9.0 on Python 3.7.6 on MacOS 10.15.3; This implies that installation instructions will be biased towards Macs, but Windows or Linux installation of software should be similar with minor modifications.

What is Sanic?

Sanic is a python 3.6+ based web framework that allows you to create an HTTP server. Its strength lies in the use of Python’s new async and await syntax released in version 3.5 allowing non-blocking code for higher performance.

Things you need

  1. Python 3.6+ and pip3 installed on your machine
  2. Virtual Env
  3. PyTest

Setting up the environment

The workflow are as follows

  1. Install Python 3.6 or higher
  2. Install Virtual Env
  3. Install PyTest

Install Python 3.6+

The standard way of doing so would be downloading the latest version from However, if you use a Mac, you can install homebrew to get the job done.

At the time of writing, the latest version of Python is 3.8.1. To install using homebrew, execute brew install python3. Homebrew will automatically install the latest Python 3 available.

Install Virtual Env

Virtual Env allows you to lock down your dependencies to a specific version and is isolated from your main system. This ensures system packages do not conflict with your project’s packages.

  1. Execute pip3 install virtualenv in terminal.
  2. Create virtual environment virtualenv (ie: virtualenv myProject)
  3. Start the virtual environment source /bin/activate (ie: source myProject/bin/activate)
  4. Install dependencies using pip3 as usual.

To exit from virtual environment use deactivate command.

Install PyTest

  1. In the activated virtual environment, execute pip3 install pytest
  2. execute deactivate && source /bin/activate. This will refresh the virtual environment once the path for PyTest has been set.

Create the First Route of your Application


from sanic import Sanic
from sanic.response import text

app = Sanic('myApplication')

async def main_route(request):
  return text('Hello')

if __name__ == "__main__":"", port=1234)

Creating a Simple Test

The biggest issue I had was trying to understand importing my web service application into Sanic test.

I placed my test at the root directory of my project folder co-locating it with just for proof of concept. In the future, I intend to put them under a “test” folder.

PyTest first look at any arguments passed on the command line when executing pytest, then inside config files for testpaths attribute before looking at the current directory recursively for files named test__*.py and *

To create a simple test, I co-located my test next to

from myApp import app

def test_default_route():
request, response = app.test_client.get('/')
result = response.body
assert response.status == 200
assert result == 'Hello'

The method I used to import my web service application is via relative imports. When code gets imported into the test file, the code is automatically executed (similar to the way JavaScript behaves). This means an instance of the Sanic application is instantiated in memory. Thus, you can use it immediately to test the endpoint.

Sanic object instances contains a method called test_client which mimicks an HTTP client making calls to an endpoint using HTTP methods. In my example, I used the get method going to the main route. You can of course change the HTTP methods to match your needs.

I hope this has helped you get started in endpoint testing with Sanic. I will be adding subsequent blog posts as I learn more about this topic and learn from best practices.

One thought on “Sanic Endpoint Testing using PyTest

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s