2019-02-17 20:53:04 +01:00
---
date: "2019-02-12:00:00+02:00"
title: "Testing"
draft: false
type: "doc"
menu:
sidebar:
parent: "development"
---
2018-12-18 17:01:46 +01:00
# Testing
2020-09-03 17:34:44 +02:00
{{< table_of_contents > }}
2022-01-23 11:49:03 +01:00
## API Tests
2018-12-18 17:01:46 +01:00
2022-01-23 11:49:03 +01:00
The following parts are about the kinds of tests in the API package and how to run them.
2018-12-18 17:01:46 +01:00
2022-01-23 11:49:03 +01:00
### Prerequesites
2018-12-18 17:01:46 +01:00
2022-01-23 11:49:03 +01:00
To run any kind of test, you need to specify Vikunja's [root path ](https://vikunja.io/docs/config-options/#rootpath ).
This is required to make sure all test fixtures are correctly loaded.
2018-12-18 17:01:46 +01:00
2022-01-23 11:49:03 +01:00
The easies way to do that is to set the environment variable `VIKUNJA_SERVICE_ROOTPATH` to the path where you cloned the working directory.
2019-04-21 20:18:17 +02:00
2022-01-23 11:49:03 +01:00
### Unit tests
2019-04-21 20:18:17 +02:00
2022-01-23 11:49:03 +01:00
To run unit tests with [mage ]({{< ref "mage.md">}} ), execute
2019-04-21 20:18:17 +02:00
2022-01-23 11:49:03 +01:00
{{< highlight bash > }}
mage test:unit
{{< / highlight > }}
2019-04-21 20:18:17 +02:00
2022-01-23 11:49:03 +01:00
In Vikunja, everything that is not an integration test counts as unit test - even if it accesses the db.
This definition is a bit blurry, but we haven't found a better one yet.
### Integration tests
2019-04-21 20:18:17 +02:00
All integration tests live in `pkg/integrations` .
2020-09-03 17:18:41 +02:00
You can run them by executing `mage test:integration` .
2019-04-21 20:18:17 +02:00
The integration tests use the same config and fixtures as the unit tests and therefor have the same options available,
see at the beginning of this document.
2020-09-03 17:18:41 +02:00
To run integration tests, use `mage test:integration` .
2020-01-26 18:08:06 +01:00
2022-01-23 11:49:03 +01:00
### Running tests with config
You can run tests with all available config variables if you want, enabeling you to run tests for a lot of scenarios.
We use this in CI to run all tests with different databases.
To use the normal config set the enviroment variable `VIKUNJA_TESTS_USE_CONFIG=1` .
### Showing sql queries
When the environment variable `UNIT_TESTS_VERBOSE=1` is set, all sql queries will be shown during the test run.
### Fixtures
All tests are run against a set of db fixtures.
These fixtures are defined in `pkg/models/fixtures` in YAML-Files which represent the database structure.
When you add a new test case which requires new database entries to test against, update these files.
#### Initializing db fixtures when writing tests
2020-01-26 18:08:06 +01:00
All db fixtures for all tests live in the `pkg/db/fixtures/` folder as yaml files.
Each file has the same name as the table the fixtures are for.
You should put new fixtures in this folder.
When initializing db fixtures, you are responsible for defining which tables your package needs in your test init function.
Usually, this is done as follows (this code snippet is taken from the `user` package):
2022-01-23 11:49:03 +01:00
{{< highlight go > }}
2020-01-26 18:08:06 +01:00
err = db.InitTestFixtures("users")
if err != nil {
log.Fatal(err)
}
2022-01-23 11:49:03 +01:00
{{< / highlight > }}
2020-01-26 18:08:06 +01:00
In your actual tests, you then load the fixtures into the in-memory db like so:
2022-01-23 11:49:03 +01:00
{{< highlight go > }}
2020-01-26 18:08:06 +01:00
db.LoadAndAssertFixtures(t)
2022-01-23 11:49:03 +01:00
{{< / highlight > }}
2020-01-26 18:08:06 +01:00
This will load all fixtures you defined in your test init method.
You should always use this method to load fixtures, the only exception is when your package tests require extra test
fixtures other than db fixtures (like files).
2022-01-23 11:49:03 +01:00
## Frontend tests
The frontend has end to end tests with Cypress that use a Vikunja instance and drive a browser against it.
Check out the docs [in the frontend repo ](https://kolaente.dev/vikunja/frontend/src/branch/main/cypress/README.md ) about how they work and how to get them running.
### Unit Tests
To run the frontend unit tests, run
{{< highlight bash > }}
2022-09-23 12:26:42 +02:00
pnpm test:unit
2022-01-23 11:49:03 +01:00
{{< / highlight > }}
The frontend also has a watcher available that re-runs all unit tests every time you change something.
To use it, simply run
{{< highlight bash > }}
2022-09-23 12:26:42 +02:00
pnpm test:unit-watch
2022-01-23 11:49:03 +01:00
{{< / highlight > }}