dwyl / dwyl/learn-zig

Testing multiple Zig files in a project

Open
#25 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
4
Forks
1
PR merge metrics
No merged PRs in 30d

Description

Tests are fully integrated in `Zig`.

You add a `test "my unit test" { .... try std.testing.expect(...)}` along your code and basically run `zig build test` if you declare a test step in your "build.zig" file.

### When you have multiple zig files, how to run all the tests through these files?

I found a Reddit thread on the subject:

```
ou need to have a common file that "references" the testing code for them to be run by a single "addTest" statement in your build file:

src/myLibrary.zig ->

pub const A = @import("A.zig");
pub const B = @import("B.zig");
pub const SomeDataType = @import("C.zig").SomeDataType;

test {
@import("std").testing.refAllDecls(@This());
}
and then in build

const main_tests = b.addTest("src/myLibrary.zig");
main_tests.setBuildMode(mode);

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&main_tests.step);
```

>[!TIP]
>Paraphrasing, the key point is to use a central file where you import all the files and do:
```zig
test {
std.testing.refAllDecls(@This());
}
```

I implemented this in `zhtml`, the `Zig` wrapper of `lexbor`, which should also be wrapped in `Elixir`.

> [!NOTE]
> You must re-export all the functions from the imported module under the namespace of your library to make them available under the library namespace.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.