fluent-asserts icon indicating copy to clipboard operation
fluent-asserts copied to clipboard

A way to include more data in an assertion.

Open BenjaminSchaaf opened this issue 7 years ago • 4 comments

When you have a test that needs to run multiple times, or simply includes an iteration of some sort, it is often useful to know at which iteration an assertion failed. Currently the only way to include that data with because:

unittest {
    foreach(i; 0..100) {
        ...

        result.should.equal(expected).because("At iteration %s".format(i));
    }
}

It would be nice if there was a simple way of including arbitrary data along with an exception, maybe something like this:

result.should.equal(expected).with(i);

----------------------
TestException: result should equal expected.
i = 5

Expected: ...

Or perhaps a middle-ground would be to allow because to be used like format:

result.should.equal(expected).because("At iteration %s", i);

BenjaminSchaaf avatar Mar 24 '18 02:03 BenjaminSchaaf

this is an interesting topic... depending on your problem, there are multiple solutions.

one way around this for your example would be this one:

static foreach(i; 0..100) {
    unittest {
        ...

        result.should.equal(expected).because("At iteration %s".format(i));
    }
}

another way is a soft assertions support, which I would like to add in the future... I'm not sure about the syntax...

gedaiu avatar Mar 24 '18 13:03 gedaiu

The issue with that workaround is that each "test" needs to happen serially, as I'm using a test runner that runs tests in parallel. What do you mean by software assertions?

BenjaminSchaaf avatar Mar 24 '18 14:03 BenjaminSchaaf

sorry ... it was a typo :P I was reffering to soft assertions the idea would be to add a bunch of assertions and evaluate them at the end of the test,

maybe this is not the best reference, but it is a way of doing it: https://github.com/Magenic/MAQS/wiki/Soft-Asserts

gedaiu avatar Mar 24 '18 15:03 gedaiu

oh... sorry. I think I misunderstood your problem. I think I get it now...

gedaiu avatar Mar 24 '18 15:03 gedaiu