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:18:41 +02:00
|
|
|
You can run unit tests with [our `Magefile`]({{< ref "mage.md">}}) with
|
2018-12-18 17:01:46 +01:00
|
|
|
|
2019-02-17 20:53:04 +01:00
|
|
|
{{< highlight bash >}}
|
2020-09-03 17:18:41 +02:00
|
|
|
mage test:unit
|
2019-02-17 20:53:04 +01:00
|
|
|
{{< /highlight >}}
|
2018-12-18 17:01:46 +01:00
|
|
|
|
2020-09-03 17:34:44 +02:00
|
|
|
{{< table_of_contents >}}
|
|
|
|
|
|
|
|
## Running tests with config
|
2018-12-18 17:01:46 +01:00
|
|
|
|
|
|
|
You can run tests with all available config variables if you want, enabeling you to run tests for a lot of scenarios.
|
|
|
|
|
|
|
|
To use the normal config set the enviroment variable `VIKUNJA_TESTS_USE_CONFIG=1`.
|
|
|
|
|
2020-09-03 17:34:44 +02:00
|
|
|
## Show sql queries
|
2018-12-18 17:01:46 +01:00
|
|
|
|
2019-04-21 20:18:17 +02:00
|
|
|
When `UNIT_TESTS_VERBOSE=1` is set, all sql queries will be shown when tests are run.
|
|
|
|
|
2020-09-03 17:34:44 +02:00
|
|
|
## Fixtures
|
2019-04-21 20:18:17 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2020-09-03 17:34:44 +02:00
|
|
|
## 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
|
|
|
|
2020-09-03 17:34:44 +02:00
|
|
|
## 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):
|
|
|
|
|
|
|
|
```go
|
|
|
|
err = db.InitTestFixtures("users")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
In your actual tests, you then load the fixtures into the in-memory db like so:
|
|
|
|
|
|
|
|
```go
|
|
|
|
db.LoadAndAssertFixtures(t)
|
|
|
|
```
|
|
|
|
|
|
|
|
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).
|