]> git.openstreetmap.org Git - nominatim.git/blob - docs/develop/Testing.md
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / docs / develop / Testing.md
1 # Nominatim Test Suite
2
3 This chapter describes the tests in the `/test` directory, how they are
4 structured and how to extend them. For a quick introduction on how to run
5 the tests, see the [Development setup chapter](Development-Environment.md).
6
7 ## Overall structure
8
9 There are two kind of tests in this test suite. There are functional tests
10 which test the API interface using a BDD test framework and there are unit
11 tests for the Python code.
12
13 This test directory is structured as follows:
14
15 ```
16  -+-   bdd         Functional API tests
17   | \
18   | +-  steps      Step implementations for test descriptions
19   | +-  osm2pgsql  Tests for data import via osm2pgsql
20   | +-  db         Tests for internal data processing on import and update
21   | +-  api        Tests for API endpoints (search, reverse, etc.)
22   |
23   +-   python      Python unit tests
24   +-   testdb      Base data for generating API test database
25   +-   testdata    Additional test data used by unit tests
26 ```
27
28 ## Python Unit Tests (`test/python`)
29
30 Unit tests for Python code can be found in the `python/` directory. The goal is
31 to have complete coverage of the Python library in `nominatim`.
32
33 To execute the tests run
34
35     py.test-3 test/python
36
37 or
38
39     pytest test/python
40
41 The name of the pytest binary depends on your installation.
42
43 ## BDD Functional Tests (`test/bdd`)
44
45 Functional tests are written as BDD instructions. For more information on
46 the philosophy of BDD testing, read the Wikipedia article on
47 [Behaviour-driven development](https://en.wikipedia.org/wiki/Behavior-driven_development).
48
49 ### General Usage
50
51 To run the functional tests, do
52
53     pytest test/bdd
54
55 You can run a single feature file using expression matching:
56
57     pytest test/bdd -k osm2pgsql/import/entrances.feature
58
59 This even works for running single tests by adding the line number of the
60 scenario header like that:
61
62     pytest test/bdd -k 'osm2pgsql/import/entrances.feature and L4'
63
64 The BDD tests create databases for the tests. You can set name of the databases
65 through configuration variables in your `pytest.ini`:
66
67  * `nominatim_test_db` defines the name of the temporary database created for
68     a single test (default: `test_nominatim`)
69  * `nominatim_api_test_db` defines the name of the database containing
70     the API test data, see also below (default: `test_api_nominatim`)
71  * `nominatim_template_db` defines the name of the template database used
72     for creating the temporary test databases. It contains some static setup
73     which usually doesn't change between imports of OSM data
74     (default: `test_template_nominatim`)
75
76 To change other connection parameters for the PostgreSQL database, use
77 the [libpq enivronment variables](https://www.postgresql.org/docs/current/libpq-envars.html).
78 Never set a password through these variables. Use a
79 [password file](https://www.postgresql.org/docs/current/libpq-pgpass.html) instead.
80
81 The API test database and the template database are only created once and then
82 left untouched. This is usually what you want because it speeds up subsequent
83 runs of BDD tests. If you do change code that has an influence on the content
84 of these databases, you can run pytest with the `--nominatim-purge` parameter
85 and the databases will be dropped and recreated from scratch.
86
87 When running the BDD tests with make (using `make tests` or `make bdd`), then
88 the databases will always be purged.
89
90 The temporary test database is usually dropped directly after the test, so
91 it does not take up unnecessary space. If you want to keep the database around,
92 for example while debugging a specific BDD test, use the parameter
93 `--nominatim-keep-db`.
94
95
96 ### API Tests (`test/bdd/api`)
97
98 These tests are meant to test the different API endpoints and their parameters.
99 They require to import several datasets into a test database. This is normally
100 done automatically during setup of the test. The API test database is then
101 kept around and reused in subsequent runs of behave. Use `--nominatim-purge`
102 to force a reimport of the database.
103
104 The official test dataset is saved in the file `test/testdb/apidb-test-data.pbf`
105 and compromises the following data:
106
107  * Geofabrik extract of Liechtenstein
108  * extract of Autauga country, Alabama, US (for tests against Tiger data)
109  * additional data from `test/testdb/additional_api_test.data.osm`
110
111 API tests should only be testing the functionality of the website frontend code.
112 Most tests should be formulated as BDD DB creation tests (see below) instead.
113
114 ### DB Creation Tests (`test/bdd/db`)
115
116 These tests check the import and update of the Nominatim database. They do not
117 test the correctness of osm2pgsql. Each test will write some data into the `place`
118 table (and optionally the `planet_osm_*` tables if required) and then run
119 Nominatim's processing functions on that.
120
121 These tests use the template database and create temporary test databases for
122 each test.
123
124 ### Import Tests (`test/bdd/osm2pgsql`)
125
126 These tests check that data is imported correctly into the place table.
127
128 These tests also use the template database and create temporary test databases
129 for each test.