Zig Exercises TDD Approach

All exercise tests currently fail when running the exercises for the first time which doesn’t help learners stick to a TDD approach to the exercises. Zig includes a way to skip tests and the following line could be added to the start of each test to better follow at TDD approach to each exercise:

test "example test" {
    if (true) return error.SkipZigTest;
    try ...
}

This way each line can easily be commented out as the learner adds more test cases.

Out of interest, why do you need if (true) here?

The zig compiler complains about unreachable code if the return error.SkipZigTest; line is added to the top of each test and the tests won’t run at all. Adding the if (true) to the beginning pleases the compiler and the test shows up as skipped as one would expect.

The proposed implementation is probably a little too quirky. Instead of relying on an always true conditional statement (which gives off a weird smell) a simple wrapper function could be used to return the appropriate SkipZigTest error:

fn skipTest() error.SkipZigTest!void {
    return error.SkipZigTest;
}

test "example test" {
    try skipTest();
    try ...
}

This approach is clearer at the low cost of an additional wrapper function in each test exercise.

1 Like

Nice, yeah. I like that. Thanks for explaining.

Anyway, yes, we’d accept a PR that added this for all exercises. It shouldn’t be there (or should be commented out) for the first test in each file!

You might want to check the exact syntax with @ErikSchierboom and/or @ee7-1282 first, but I’d like to see this added to the track sooner rather than later :slight_smile:

1 Like

I made a PR implementing the skipTest function and added the line to each Zig exercise test suite (excluding each test suite’s first test). Could you take a look?

Maybe keep current behavior but add simple switch to enable this skipping?