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

Replace std.debug.assert with std.testing.expect in tests #31

Closed
MorganMcmillan opened this issue Jul 4, 2024 · 0 comments
Closed

Replace std.debug.assert with std.testing.expect in tests #31

MorganMcmillan opened this issue Jul 4, 2024 · 0 comments

Comments

@MorganMcmillan
Copy link
Contributor

When a test fails as a result of std.debug.assert recieving false, it produces a rather large error message:

thread 11119 panic: reached unreachable code
zig/lib/std/debug.zig:412:14: 0x103eb9d in assert (test)
    if (!ok) unreachable; // assertion failure
             ^
zig-string/zig-string-tests.zig:24:21: 0x103e25f in test.Basic Usage (test)
    std.debug.assert(myString.cmp("🔥 Hello, World "));
                    ^
zig/lib/compiler/test_runner.zig:155:25: 0x1057a99 in mainTerminal (test)
        if (test_fn.func()) |_| {
                        ^
zig/lib/compiler/test_runner.zig:35:28: 0x104c7db in main (test)
        return mainTerminal();
                           ^
zig/lib/std/start.zig:505:22: 0x10479f9 in posixCallMainAndExit (test)
            root.main();
                     ^
zig/lib/std/start.zig:258:5: 0x1047561 in _start (test)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
zig-string/.zig-cache/o/eb36f7fdef19d5a1c7c2da449e45015e/test

But when try std.testing.expect is used instead, the error message looks like this:

1/7 zig-string-tests.test.Basic Usage...FAIL (TestUnexpectedResult)
zig/lib/std/testing.zig:542:14: 0x103e15f in expect (test)
    if (!ok) return error.TestUnexpectedResult;
             ^
Zig Projects/zig-string/zig-string-tests.zig:24:5: 0x103e3c4 in test.Basic Usage (test)
    try expect(myString.cmp("🔥 Hello, World "));
    ^
6 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
zig-string/.zig-cache/o/eb36f7fdef19d5a1c7c2da449e45015e/test

Not only is the message shorter, but it's also clearer that a test failed, and where it failed.

std.debug.assert should not be used in testing, as it's meant for debugging programs. Always use std.testing.expect in tests to assert that a condition is true.

@MorganMcmillan MorganMcmillan changed the title Replace std.debug.assert with std.testing.expect Replace std.debug.assert with std.testing.expect in tests Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants