When compiling a test block, where does the @import("root") comes from?

I have written a library that uses @import("root") extensively and would like to write some tests. When writing a test block, where should I put the configuration I usually put in main.zig ?

The test runner is "root".

If possible, configure via build options instead of root.

Otherwise, you can pass your own test runner through .test_runner in addTests’ options.

You can just copy and modify the default one, located [lib dir from 'zig env']/compiler/test_runner.zig.

Unfortunately, I can’t see of a way to pass along any custom information to a test runner through the build system. You’d have to hard code it or configure it through some other means.

So I must learn how to use build options. Where to find more information about that ? Also, I believe I need to modify build.zig to add a dedicated test build step ?

passing options to your code, getting options from the cli
Most of that page needs updating, but those are luckily still the same.

The limitation and reason to configure with the root module are when you configuration needs to reference the contents of the module and you cant remove the contents into options. An example of this, providing a custom log fn for std.log, cant access functions out side of the source.

I assume your using zig test, you can still specify a custom test runner via --test-runner /path/to/test/runner.zig.

to add a test step looks like that part has been updated :slight_smile:, it’s more complex than it needs to be, supporting a list of targets to test.

Havent checked the whole build documentation, but it seems to have been updated since last i saw it, at least the parts i linked. parts could still be outdated.

1 Like

I ended up not making a custom test runner, options in build.zig were good enough for me. It’s an awesome way to test with different build configuration for example.