Newbie using std.testing: notEquals and messages

I am trying out the testing package and it seems to be missing:

  • Equivalent “notEquals” for the various existing tests;
  • Adding a message to be printed when a test fails

In regards ti the first point I am currently using a try std.testing.expect(!r); where r is coded explicitly. What is the standard way doing this? To use the existing tests, do I have to use an if to get the error and use that for negation?

As for the second issue, is there any way to code this cleanly without the if/else for error checking?

TIA

For your first point, I don’t think there’s a perfectly agreed-upon standard way to do this, but the most intuitive way I think would be to just do std.testing.expect(a != b). You could also make your own expectNotEqual function, but either way would be fine IMO.

For your second point, you could use a little helper function like this:

fn printIfError(maybe_err: anyerror!void, comptime fmt: []const u8, args: anytype) !void {
    maybe_err catch std.debug.print(fmt, args);
    return maybe_err;
}

which would be used like this:

try printIfError(std.testing.expect(hopefully_true), "oh no! {} {s}\n", .{ 1234, "xyz" });
1 Like

What I usually do:

std.testing.expectEqual(foo, bar) catch |err| {
    std.debug.print("message\n", .{});
    return err;
};
1 Like

After reading your question again, I realize that you’re not talking about notEquals in particular, but a negation of all tests in general.

For that, I’m again not aware of a standard technique, but would lean towards manually making negated versions of some of the functions, especially ones that emit console messages (like expectEqualStrings), as the messages would be wrongly printed when the test succeeds if you were to lazily reappropriate the existing functions.

Do you mind giving a bit more detail as to what r is in this case? I also thought the current functions were missing the negations as well, but I found that all the cases where I would use them were not providing any real value since I needed deterministic output, but your case may be different and shine some light on how they could be useful.

At the end of the day though

if (actual == not_expected) {
    std.debug.print("debug message", .{});
    return error.TestExpectedNotEqual;
}

Is how I was doing it before I removed my negative tests. I would take a look to see if it’s possible to have your tests be positive assertions, because I think in the overwhelming majority of cases the positive assertion is much stronger of a test.

1 Like

@pink-lemons Currently I am comparing strings, so its not a problem. However, I am thinking that many of the existing testing functions may be useful and the community may have some predefined way of using them in negated form.

And yes. I agree that most cases I use are not negations. In this case I am checking that a value has been initialized - not the default.

I am marking this as a solution because it was the first that essentially answers my questions. This does not mean that the other suggestions are incorrect or less useful.

@milogreg Thanks.

@squeek502 This print solution seems “cleaner” than a function. Going to see if its easier/simpler than a call like the one above. And a simple ! may suffice for negation.