Idris2/libs/test
2021-05-27 11:12:44 +01:00
..
Test [ test ] cosmetic improvements 2021-05-26 19:50:50 +01:00
Makefile [ docs ] Build docs for the 'test' package 2021-05-27 11:12:44 +01:00
README.md [ doc ] Update the test-lib's readme according to the current state 2021-05-14 11:46:11 +01:00
test.ipkg Add a test package to the Idris 2 project (#1162) 2021-03-09 18:27:05 +00:00

Test

The test package exposes the same test framework(s) the Idris 2 compiler uses for its test suite.

In a language like Idris 2, there are a number of strategies one can take for testing their code and the eventual goal of this testing package is to facilitate a blend of these strategies in any project. Currently the package contains one module facilitating one style of testing: Golden. Contributions containing other modules that enable additional testing styles are encouraged.

To use the test package, either pass -p test to the idris2 executable or add depends = test to your test suite's package file.

Golden

Golden facilitates testing by way of comparing test output with a predetermined expecation. The module is well documented in its own source code but the following is a primer.

You first import the Test.Golden module and write an IO function to serve as the entrypoint for your test suite. This function must at some point call into Golden's runner.

-- your_project/tests/Main.idr

module Main

import Test.Golden

tests : TestPool

main : IO ()
main = do
  runner [tests]

You populate the TestPool list that the runner expects with one entry per pool of tests you want to run. Within each pool, tests are run concurrently.

tests : TestPool
tests = MkTestPool "Description of the test pool" [] [
  "my_great_test"
]

The first argument to MkTestPool is a description of the whole test pool. It will be printed before the tests from this pool are started.

The second argument to MkTestPool (empty in the above example) is a list of codegen backends required to run the tests in the given pool. Any empty list means no requirements. If your tests required the Racket backend, you could instead specify [Racket]. See the Requirement type for more.

The third argument to MkTestPool is a list of directory names that can be found relative to your Main.idr file. This directory will have some combination of the following files.

my_great_test/
  Test.idr
  test.ipkg
  expected
  input
  run

These files define:

  1. Any Idris 2 source code needed for the test (Test.idr, which can be named anything you'd like and is not limited to 1 file).
  2. Any package information needed to build those source files (test.ipkg).
  3. The command run at the shell to execute your test (run).
  4. Optional input passed to your test case (input).
  5. The expected output of running your test (expected).

See the documentation in Test/Golden.idr and the template directories provided with the Idris 2 project for a great primer on these files.

When you run your tests (the executable produced by building your tests/Main.idr file), you need to specify the Idris executable to use and optionally use interactive mode (--interactive) or limit the test cases that are run (--only [names]).

Interactive mode is useful when you know the expected output for a test case is going to change -- you will be prompted to updated the expectation so you can choose whether the output produced by a new test run should become the new "golden" standard. You can even skip the step of creating an expected file altogether when you write a new test case and use interactive mode to accept the output of your test case as the expectation.