# Running tests ## Unit-tests The unit tests are defined using the [googletest] and [rapidcheck] frameworks. [googletest]: https://google.github.io/googletest/ [rapidcheck]: https://github.com/emil-e/rapidcheck ### Source and header layout > An example of some files, demonstrating much of what is described below > > ``` > … > ├── src > │ ├── libexpr > │ │ ├── … > │ │ ├── value > │ │ │ ├── context.cc > │ │ │ └── context.hh > │ … … > ├── tests > │ … > │ └── unit > │ ├── libcmd > │ │ └── args.cc > │ ├── libexpr > │ │ ├── … > │ │ └── value > │ │ ├── context.cc > │ │ └── print.cc > │ ├── libexpr-support > │ │ └── tests > │ │ ├── libexpr.hh > │ │ └── value > │ │ ├── context.cc > │ │ └── context.hh > │ ├── libstore > │ │ ├── common-protocol.cc > │ │ ├── data > │ │ │ ├── libstore > │ │ │ │ ├── common-protocol > │ │ │ │ │ ├── content-address.bin > │ │ │ │ │ ├── drv-output.bin > … … … … … … > ``` The unit tests for each Lix library (`liblixexpr`, `liblixstore`, etc..) live inside a directory `src/${library_shortname}/tests` within the directory for the library (`src/${library_shortname}`). The data is in `tests/unit/LIBNAME/data/LIBNAME`, with one subdir per library, with the same name as where the code goes. For example, `liblixstore` code is in `src/libstore`, and its test data is in `tests/unit/libstore/data/libstore`. The path to the unit test data directory is passed to the unit test executable with the environment variable `_NIX_TEST_UNIT_DATA`. ### Running tests You can run the whole testsuite with `just test` (see justfile for exact invocation of meson), and if you want to run just one test suite, use `just test --suite installcheck functional-init` where `installcheck` is the name of the test suite in this case and `functional-init` is the name of the test. To get a list of tests, use `meson test -C build --list` (or `just test --list` for short). For `installcheck` specifically, first run `just install` before running the test suite (this is due to meson limitations that don't let us put a dependency on installing before doing the test). Finer-grained filtering within a test suite is also possible using the [--gtest_filter](https://google.github.io/googletest/advanced.html#running-a-subset-of-the-tests) command-line option to a test suite executable, or the `GTEST_FILTER` environment variable. ### Unit test support libraries There are headers and code which are not just used to test the library in question, but also downstream libraries. For example, we do [property testing] with the [rapidcheck] library. This requires writing `Arbitrary` "instances", which are used to describe how to generate values of a given type for the sake of running property tests. Because types contain other types, `Arbitrary` "instances" for some type are not just useful for testing that type, but also any other type that contains it. Downstream types frequently contain upstream types, so it is very important that we share arbitrary instances so that downstream libraries' property tests can also use them. It is important that these testing libraries don't contain any actual tests themselves. On some platforms they would be run as part of every test executable that uses them, which is redundant. On other platforms they wouldn't be run at all. ### Characterization testing See [below](#characterization-testing-1) for a broader discussion of characterization testing. Like with the functional characterization, `_NIX_TEST_ACCEPT=1` is also used. For example: ```shell-session $ _NIX_TEST_ACCEPT=1 just test --suite check libstore-unit-tests ... ../tests/unit/libstore/common-protocol.cc:27: Skipped Cannot read golden master because another test is also updating it ../tests/unit/libstore/common-protocol.cc:62: Skipped Updating golden master ../tests/unit/libstore/common-protocol.cc:27: Skipped Cannot read golden master because another test is also updating it ../tests/unit/libstore/common-protocol.cc:62: Skipped Updating golden master ... ``` will regenerate the "golden master" expected result for the `liblixstore` characterization tests. The characterization tests will mark themselves "skipped" since they regenerated the expected result instead of actually testing anything. ## Functional tests The functional tests reside under the `tests/functional` directory and are listed in `tests/functional/meson.build`. Each test is a bash script. ### Running the whole test suite