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
Setting up the environment
The workflow are as follows
- Install Python 3.6 or higher
- Install Virtual Env
- Install PyTest
Install Python 3.6+
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.
pip3 install virtualenvin terminal.
- Create virtual environment
- Start the virtual environment
- Install dependencies using pip3 as usual.
To exit from virtual environment use
- In the activated virtual environment, execute
pip3 install pytest
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
# myApp.py from sanic import Sanic from sanic.response import text app = Sanic('myApplication') @app.route("/") async def main_route(request): return text('Hello') if __name__ == "__main__": app.run(host="0.0.0.0", 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 myApp.py 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
To create a simple test, I co-located my test next to myApp.py
# test_myApp.py from myApp import app def test_default_route(): request, response = app.test_client.get('/') result = response.body assert response.status == 200 assert result == 'Hello'
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.