Files
Build-System/docs/build-system/consumer/testing.md
T

160 lines
6.7 KiB
Markdown

---
title: Tests, Fixtures and Reports
description: Register VOID_TEST cases, declare test targets, use labels, resources and fixtures, and publish JUnit, CDash and coverage results
section: Testing & Reports
badge: vbs test
order: 1
tags: [testing, void-test, fixtures, labels, junit, cdash, coverage, wine]
updated: 2026-12-06
---
# Tests, Fixtures and Reports
VBS runs tests as ordinary `TargetKind::Test` executables. There is no separate test DSL: a test target is built by the normal graph, then the runner schedules test executables in parallel up to `--jobs`, subject to fixtures and exclusive resources. A test passes when its process exits with status `0`.
## Register tests
VoidLibrary's `VOID_TEST(Suite, name)` macro self-registers a case, so no external scanner is needed:
```cpp
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
VOID_TEST(Math, addition_works) {
Void::Testing::expect_eq(1 + 1, 2);
}
```
Declare a test target in `build.cpp`:
```cpp
auto tests = project.test("math_tests");
tests.cxx_standard(Void::Build::CxxStandard::Cxx26)
.sources({"tests/math_test.cpp"})
.link(core)
.link(library.target("Void::Testing"))
.label("unit");
```
`vbs gen-tests --input FILE --output-dir DIR` emits a registration unit for cases that do not use the self-registering macro. It is also what a CMake-free test setup can use to avoid a scanner.
## Run and select
```sh
vbs test
vbs test --profile release --target math_tests
vbs test --label unit
vbs test --label unit --label integration
vbs test --jobs 8 --test-timeout 120
```
- `--target` selects target names; repeatable.
- `--label` selects tests carrying any listed label. Fixture setup/cleanup targets are pulled in automatically even when their own labels do not match.
- `--test-timeout` is per test, default `300` seconds, allowed `1..86400`.
- A failed fixture setup skips its dependents with an explicit `SKIPPED` line.
- The final summary prints `PASSED`/`FAILED` per test and exits `1` if any test failed.
- `vbs test` does not forward arguments to test executables. To use `Void::Testing` filters or its own reporters, run the test binary directly.
## Fixtures, resources and parallelism
```cpp
auto db_setup = project.test("db_setup");
db_setup.label("fixture").fixture_setup("database");
auto db_test = project.test("db_test");
db_test.link(core)
.label("integration")
.resource("database")
.fixture_required("database");
auto db_cleanup = project.test("db_cleanup");
db_cleanup.label("fixture").fixture_cleanup("database");
```
- **Resources**: tests declaring the same resource never overlap. The resource is held exclusively for the duration of the test; other tests continue in parallel.
- **Fixtures**: setup runs before every user, required fixtures pull their setup in, cleanup runs after all users, and setup failure skips dependents. Cycles are rejected.
- **Ordering** is deterministic for the fixture graph; independent tests may complete in any order.
This matches the CTest fixture/resource model closely enough to migrate existing suites without rewriting their topology.
## JUnit XML
```sh
vbs test --junit build/test-results.xml
```
The report aggregates every executed test **target** across selected profiles. Each profile becomes a `<testsuite>`; each target becomes a `<testcase>` with `classname`, `name`, `time`, a `<failure>` with the exit status and captured output when it fails, and escaped XML text. It does not expand individual `VOID_TEST` cases into separate testcases. This is directly consumable by Gitea Actions, GitHub Actions and most CI dashboards.
## CDash
```sh
vbs test --cdash https://cdash.example.com \
--cdash-project my-project \
--cdash-build linux-debug \
--cdash-output build/Test.xml
```
- `Test.xml` is CTest-compatible and includes the site, build name/stamp, OS and generator.
- `--cdash` submits a multipart form to `<cdash>/submit.php?project=...`.
- The upload uses `Void::Net` when linked, otherwise the POSIX HTTP/1.1 fallback.
- `--cdash-output` writes the same XML locally without uploading.
## Coverage
```sh
vbs test --coverage build/coverage
```
Every compile and link for the selected profiles gets `--coverage`. After the tests run, VBS walks the profile build directories for `.gcda` files, invokes `gcov --json-format`, filters out sources outside the project root, and writes:
- `build/coverage/coverage.info`: lcov records (`TN`, `SF`, `DA`, `LF`, `LH`, `end_of_record`);
- `build/coverage/summary.txt`: one line with percent, hit lines, total lines and file count.
`gcov` must be on `PATH`. A run that produces no `.gcda` data is an explicit failure, not an empty report.
## Cross-platform tests
A Windows target can be built on Linux with MinGW-w64 and executed under Wine:
```sh
vbs test --target-os windows --target-arch x86_64 --target app_tests
```
The toolchain driver selects `x86_64-w64-mingw32-g++`, emits PE artifacts and runs the test through the configured Wine runner. The repository regression test is skipped when MinGW or Wine is unavailable. Windows-specific validation (reserved device names, case-insensitive output collisions and case-insensitive file lookup) is exercised by that path.
## CTest during bootstrap
The CMake bootstrap exposes eight CTest entries:
| Test | Scope |
| --- | --- |
| `vbs_sdk_tests` | Declaration SDK and example description |
| `vbs_core_tests` | SHA-256, process supervisor, profiles, snapshots, state, end-to-end |
| `vbs_v1_tests` | CompDB, providers, packages, install, modules, daemon, signing, IDE, workspaces |
| `vbs_registry_tests` | Registry API, RBAC, CAS, workers and live CLI E2E |
| `vbs_selfhost_tests` | Native self-host, `VOID_TEST` generation, bit-identical rebuilds |
| `vbs_sdk_registration` | Self-registration and named JSON cases |
| `vbs_cli_contract` | Auto-generated CLI help, subcommands and error contracts |
| `vbs_description_contract` | Four profile/compiler contexts and describe error contracts |
```sh
dev cpp-gcc -c ctest --test-dir build --output-on-failure
```
Under `vbs test`, the same suite executables are declared as test targets, so the self-hosted build runs them without CMake.
## Reporting and CI checklist
:::steps
@@step[Run targeted tests locally]
`vbs test --label unit --jobs 8`
@@step[Produce CI reports]
`vbs test --junit build/junit.xml --coverage build/coverage --cdash-output build/Test.xml`
@@step[Fail the pipeline on any test]
Use the `vbs test` exit status; do not parse only the logs.
@@step[Keep fixtures explicit]
Prefer a resource for exclusive services and a fixture for setup/cleanup ordering.
:::
Next: [use the remote action cache and workers](/build-system/consumer/remote-ci) or [read the self-hosting verification details](/build-system/developer/self-hosting).