zig icon indicating copy to clipboard operation
zig copied to clipboard

missing newline after `Test [1/1] ...`

Open baronleonardo opened this issue 2 years ago • 9 comments

Zig Version

0.10.1

Steps to Reproduce and Observed Behavior

test "testing"
{
    std.log.warn("test", .{});
}

output:

Test [1/1] test.testing... [default] (warn): test

Expected Behavior

output:

Test [1/1] test.testing...
[default] (warn): test

baronleonardo avatar Jun 10 '23 06:06 baronleonardo

behavior changed in master, tests are not currently expected to output https://github.com/ziglang/zig/issues/15091

also related https://github.com/ziglang/zig/issues/5738

edit there was another related issue but i cant find it atm

nektro avatar Jun 11 '23 01:06 nektro

test blocks are meant to be whats commonly referred to as "unit tests" and should generally not have observable side effects

nektro avatar Jun 11 '23 01:06 nektro

@nektro the warn and error log levels should make tests fail though, so I could see why this would be desired (also, it’s helpful when debugging tests)

haze avatar Jun 11 '23 02:06 haze

  • during first stage of developing a module I need to test it by just printing out to see (I failed to debug unit tests in zig so I;m using the old school way printing)
  • if you have a print function how do you test it ?

baronleonardo avatar Jun 11 '23 04:06 baronleonardo

during first stage of developing a module I need to test it by just printing out to see (I failed to debug unit tests in zig so I;m using the old school way printing)

What I do:

  • If I know my test will be printing something, I sometimes emit a newline at the start of the test to get the first line to be on a separate line from the Test [1/1] test.testing.... For the example in the OP that could be:
test "testing" {
    std.debug.print("\n", .{});
    std.log.warn("test", .{});
}
  • Otherwise, I just keep in mind that the first line of output will go on the same line as the Test [1/1] test.testing...

if you have a print function how do you test it ?

Either:

  • Write the function so that it can output to more than just stderr/stdout (like a std.io.Writer instead) and then in the tests you can use e.g. an ArrayList as a buffer to write into instead of outputting to stderr/stdout
  • Use a child process to spawn the thing that prints, collect the stdout/stderr, and check it. This is the strategy used by the compare output Zig tests.

squeek502 avatar Jun 11 '23 04:06 squeek502

thanks @squeek502 for your ideas but they are workarounds to me not solutions we could solve all of that by just printing out normally

baronleonardo avatar Jun 11 '23 05:06 baronleonardo

It's not as simple as 'printing out normally' since the Test [1/1] test.testing... is technically part of a progress bar. If you have multiple tests the Test [1/5] test.testing... would get overwritten with Test [2/5] test.test2... and so on. If none of the tests print to stderr, then the final result of zig test ends up looking like this:

> zig test multiple-tests.zig
All 7 tests passed.

squeek502 avatar Jun 11 '23 05:06 squeek502

while developing you can also put it in a pub fn main() !void {} and move to test {} once it is ready

nektro avatar Jun 11 '23 06:06 nektro

can we add for example std.testing.print function ?

baronleonardo avatar Jun 11 '23 14:06 baronleonardo