Testing Setup for REST API

Learn how to create a setup for testing the REST API Application.

Setup for testing

The REST API application is implemented with persistent storage using MySQL Database. Let’s test it to ensure that the application can be used correctly and effectively.

Before testing the application, the following libraries are installed:

  • apitest: Used to create a test case for the API application.

  • Faker: Used to create fake data. You can install the libraries using the following commands on a local machine. However, they have already been set up for you on the Educative platform.

go get -u github.com/steinfletcher/apitest
go get -u github.com/bxcodec/faker/v3

After installing the libraries, a new database is created for testing purposes.

CREATE DATABASE inventory_test;

We create the testing database so the main application isn’t affected and continues to run effectively.

Note: It’s recommended to use the same database type for testing and for running the application. For example, the Postgres database is used to test and run the application.

Create a faker

The faker component is used to create a sample of fake data for testing purposes. In this case, two fakers will be created, one for the user and one for the item data.

Before creating a faker, we specify the attributes that will be filled by the faker. In the models directory, we add some faker tags for the item model in the item.go file.

Get hands-on with 1200+ tech skills courses.