8000 Integration tests · fictadvisor/fictadvisor Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Integration tests

Sviatoslav Shesterov edited this page Jun 17, 2023 · 3 revisions

Introduction

What is integration tests? What is the difference with unit tests? What are the requirements of unit testing for our project? We will discuss it here.

Integration testing is the testing of full logic of our services, or at least as much as you can. The rule of the integration tests is - the less mocks the better. You don't need to mock database. The only services that must be mocked is telegram api, mail services and other part of our back-end service that aren't related to it explicitly.

Requirements

The main requirements of integration tests is our database. For such purposes there is created docker-compose.yml file, which contains information of how to start postgresql database.

If you are a Windows user, you need to install Docker Desktop. For linux users there is many repositories with Docker, you can Google it. Any of them are suitable.

Pay attention! Check whether you have installed PostgreSQL database explicitly. Our docker compose runs on 5432 port, that is the default port of PostgreSQL database. Before you start docker compose file, stop your postgresql instance on the machine.

Managing database

To start docker compose file, enter:

$ docker compose up

But starting PostgreSQL container is not enough, because there is no any tables, databases, data for tests, etc. The second stage of starting database is migration.

What is the migration? It's an initiation of our database by creating neccessary databases, columns, foreign keys, connections, etc. Migration allow developers to have clear history of database structure. It's really helpful when you have more than one database in your CI/CD and you need to have exact changes. But dumping data is also required :3

To migrate database, enter:

$ yarn test:migrate

Okay, database is ready, but what we need to test if there is no data? Well, seeding will help us with that.

What is the seeding? It's a creating of default data for our database. But seeding for our purpose is not really neccessary, because we will create data in each module separately to have a deterministic set of data. In our seeding we creates global variables, for example semester dates and poll borders.

To start seeding, enter:

$ yarn test:seed

Environment of integration testing

We have a .testing.env file that is available in our public repository. You can add new values if you need something to store.

Pay attention! Don't store .env file. Testing environment can be corrupted after that. Use .development.env instead.

Integration tests

Integration tests are stored in files with .ispec.ts extension. It's required, because configuration of integration tests is determined in jest.integration.config.json, where you can see an extension of file.

To start tests, enter:

$ yarn test:integration

If everything is okay, all tests will run properly.

How to write integration tests?

Well, it's a quite difficult question. You can see the examples of integration tests on our project from other developers.

Before integration tests, it's neccessary to create testing data. You can do this in .beforeAll() method. You must delete all data that you have created in .afterAll(). If you want, you can create data before any method, that you want.

Actions after tests

Considering you have created database, migrated it, add seeding data, you need to stop container and restart it. So you will be sure that you have a clear testing environment.

To stop container, enter:

$ docker compose down
0