Sign in

These days writing automated tests for your software is just as important as any other phase in the development life cycle.

In this article, I’ll go over how I write unit tests for HTTP servers in Go.

Install stretchr/testify

To make testing easier, I’ll be using the stretchr/testify, which gives access to many testing utils.

After initializing your go project, run the following command to install the testing library:

$ go get -u github.com/stretchr/testify

Testing http.HandlerFunc

An http.HandlerFunc is a function that will be called when a request hits its corresponding endpoint, and has the following signature:

type HandlerFunc func(ResponseWriter, *Request)

In this example…


Testing is just as important as any other phase of the software development life cycle, yet many developers tend to neglect it.

In this article, I’ll go through the implementation of some simple unit tests for a small Flask application that supports two routes.

The Flask application I’ll be writing tests for can be found in this repo: https://github.com/ElMehdi19/flask-schema-example

Install Pytest

After cloning the repo, setting up a virtual environment, and installing the requirements, let’s now install Pytest:

$ pip3 install pytest

Let’s now create a new folder in the root of the project where our tests will live:

$ mkdir testing


Consider a phenomenon characterized by 2 variables x and y.

After observing the phenomenon, we now dispose of a sample dataset.

Goal: we want to find a mathematical formula that links x to y

Mehdi

Mechanical engineering student — software developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store