Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure zig build test includes all tests #22

Closed
hendriknielaender opened this issue Nov 24, 2023 · 3 comments
Closed

Ensure zig build test includes all tests #22

hendriknielaender opened this issue Nov 24, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@hendriknielaender
Copy link
Owner

It has come to our attention that not all unit tests are being executed when the zig build test command is invoked. This command is the standard way to run tests in a zig project, and it's expected to compile and execute all test cases defined across the project. However, some tests appear to be omitted during this process.

Expected Behavior

The zig build test command should automatically find and run all unit tests in the project, regardless of their location within the codebase. This ensures a consistent and reliable testing process, which is crucial for maintaining code quality and stability.

Suggested Solution

I propose an adjustment to the build.zig file to ensure that all unit tests are included when running zig build test. This may involve reviewing the build configurations to guarantee that each test target is appropriately identified and included in the test run.

@hendriknielaender hendriknielaender added the bug Something isn't working label Nov 24, 2023
@jasperdunn
Copy link

jasperdunn commented Nov 25, 2023

I've seen this pattern used a bunch, though I'm sure Zig will eventually have a better solution for it:

// src/tests.zig

comptime {
    _ = @import("foo.zig");
    _ = @import("bar.zig");
    _ = @import("baz.zig");
}
// build.zig

const unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/tests.zig" },
        .target = target,
        .optimize = optimize,
    });

@FObersteiner
Copy link
Collaborator

FObersteiner commented Nov 26, 2023

Another option is to handle tests like we handle the examples, i.e. via the build-script. Suppose you have 3 zig files with tests in direcory ./src, you would define their names like

const test_files = [_][]const u8{ "test_a", "test_b", "test_c" };

then call to run each of them in the build.zig with

const test_step = b.step("tests", "Run library tests");
for (test_files) |test_name| {
    const _test = b.addTest(.{
        .name = test_name,
        .root_source_file = .{ .path = b.fmt("src/{s}.zig", .{test_name}) },
        .target = target,
        .optimize = optimize,
    });
    const run_test = b.addRunArtifact(_test);
    test_step.dependOn(&run_test.step);
}

That seems quiet clear to me, just look at the build.zig an you know what's happening; no nested calls within source code files to other files etc.


Addendum, I tried to find a way to avoid caching for tests. One option is to completely "disable" caching by dumping the build cache to some temp directory. However, that is not what we want I think. I also set up a question on ziggit. For now, IMHO the best option is to keep benchmark in "tests", but compile those to binaries that you can run repeatedly if desired - or live with the fact that tests only run once, after you change the code that is tested. On the long run, maybe benchmarks can become a std lib feature :)

@hendriknielaender
Copy link
Owner Author

Close this issue since, now all tests should be included.

Opened a new issue regarding the caching issue for the benchmark test #26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants