Top level directory structure:
src: Testing code.suite: Input files. Mostly organized in parallel to the code. Each file can contain multiple tests, each of which is a section of Typst code following--- {name} {attr}+ ---.ref: References which the output is compared with to determine whether a test passed or failed.store: Store for PNG, PDF, and SVG output files produced by the tests.
Running all tests (including unit tests):
cargo test --workspaceRunning just the integration tests (the tests in this directory):
cargo test --workspace --test testsThe repository includes the alias cargo testit to make this less verbose. In
the examples below, we will use this alias.
Running all tests with the given name pattern. You can use regular expressions.
cargo testit math # The name has "math" anywhere
cargo testit math page # The name has "math" or "page" anywhere
cargo testit "^math" "^page" # The name begins with "math" or "page"
cargo testit "^(math|page)" # Same as above.Running all tests discovered under given paths:
cargo testit -p tests/suite/math/attach.typ
cargo testit -p tests/suite/model -p tests/suite/textRunning tests that begin with issue under a given path:
cargo testit "^issue" -p tests/suite/modelRunning a test with the exact test name math-attach-mixed.
cargo testit --exact math-attach-mixedYou may find more options in the help message:
cargo testit --helpBy default, the integration tests run all stages, which also generates PDFs
and SVGs. To make them go faster while developing, you can pass the --stages
flag to only run certain targets/outputs. The available stages are the following
ones:
paged: Compile the paged target and producerender,pdf, andsvgoutputs.render: Producerender(png) output.pdf: Producepdfoutput.pdftags: Producepdftagsoutput.svgProducesvgoutput.
html: Compile thehtmltarget and producehtmloutput.
You can specify multiple pages, separated by commas:
cargo testit --stages html,pdftagsThe syntax for an individual test is --- {name} {attr}+ --- followed by some
Typst code that should be tested. The name must be globally unique in the test
suite, so that tests can be easily migrated across files. A test name is
followed by space-separated attributes. At least one test target must be
specified. For instance, --- my-test html --- adds the html target to
my-test, instructing the test runner to test HTML output. The following
attributes are currently defined:
paged: Tests paged output against a reference image.html: Tests HTML output against a reference HTML file.pdftags: Tests the output of the PDF tag tree.pdfstandard({standard}): Sets the PDF standard used for testing PDFs and the PDF tag tree.large: Permits a reference image size exceeding 20 KiB. Should be used sparingly.
There are, broadly speaking, three kinds of tests:
-
Tests that just ensure that the code runs successfully: Those typically make use of
testorassert.eq(both are very similar,testis just shorter) to ensure certain properties hold when executing the Typst code. Generic scripting tests that don't depend on the target should usepagedas the test target. -
Tests that ensure the code emits particular diagnostic messages: Those have inline annotations like
// Error: 2-7 thing was wrong. An annotation can start with either "Error", "Warning", or "Hint". The range designates the code span the diagnostic message refers to in the first non-comment line below. If the code span is in a line further below, you can write ranges like3:2-3:7to indicate the 2-7 column in the 3rd non-comment line. -
Tests that ensure certain output is produced:
-
Visual output: When a test has the
pagedattribute, the compiler produces paged output, renders it with thetypst-rendercrate, and compares it against a reference image stored in the repository. The test runner automatically detects whether a test has visual output and requires a reference image in this case.To prevent bloat, it is important that the test images are kept as small as possible. To that effect, the test runner enforces a maximum size of 20 KiB. If you're updating a test and hit
reference output size exceeds, see the section on "Updating reference images" below. If truly necessary, the size limit can be lifted by adding alargeattribute after the test name, but this should be the case very rarely. -
HTML output: When a test has the
htmlattribute, the compiler produces HTML output and compares it against a reference file stored in the repository. -
PDF tags output: When a test has the
pdftagsattribute, the compiler produces a human readable tag tree formatted as a YAML document and compares it against a reference file stored in the repository.
-
If you have the choice between writing a test using assertions or using reference images, prefer assertions. This makes the test easier to understand in isolation and prevents bloat due to images.
If you created a new test or fixed a bug in an existing test, you may need to
update the reference output used for comparison. For this, you can use the
--update flag:
cargo testit --exact my-test-name --updateYou can also pass the --stages flag to only update specific reference output.
See Test stages for more details.
cargo testit --exact my-test-name --update --stages=renderFor visual tests, this will generally generate compressed reference images (to remain within the size limit).
If you use the VS Code test helper extension (see the tools folder), you can
alternatively use the save button to update the reference output.