Postmortem: Template unittests are bad & you shouldn't catch Error

Jacob Carlborg doob at me.com
Fri Oct 23 09:17:37 UTC 2020


On Thursday, 22 October 2020 at 19:18:03 UTC, Steven 
Schveighoffer wrote:

> I thought AssertErrors during unit tests are allowed to be 
> caught by the unit test framework?

Technically the spec doesn't say anything about that. What it 
does say is that if an assertion fails inside a `unittest` block 
the program is still in a valid state. I'm not sure that means 
you can catch `AssertError` or not. It's also not clear if it 
only applies to `assert` used directly inside a `unittest` block 
or any code reachable through a `unittest` block.

> Isn't assert the main tool to test things?

Yes.

> If you aren't supposed to catch them ever, how does unittesting 
> work?

Yes, exactly. If we should really follow that to the letter 
(which I don't think we should) then the program would abort as 
soon as a test failed. Then you would have to fix that failing 
test to see the rest of the failing tests.

> The stack may not be unwound. That doesn't mean the program is 
> in an invalid state, especially if you control all the code you 
> are testing.

Exactly, see above.

[1] https://dlang.org/spec/unittest.html

--
/Jacob Carlborg


More information about the Digitalmars-d mailing list