www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can one customize unit tests?

reply PV <pavel_vozenilek hotmail.com> writes:
Is it possible to somehow customize syntax and running of unit 
tests?



How I could use it:

1. Running only the tests from recently modified source files.
2. Ability to add optional timeout constraint into the tests, and 
then check whether the test doesn't exceed it.

     unittests (time < 20 ms) { ... }

3. Checking that tests do not leak memory (where applicable).
4. Invoking tests in both D code and in C, where similar 
framework exists.


I'd looked into the documentation, found nothing, but perhaps I 
just missed it. If it is not possible, is it realistic to (very 
easily) hack the D compiler to implement such feature?


Would such feature also work in betterC?
Aug 03 2019
parent Dennis <dkorpel gmail.com> writes:
The out-of-the box unittest runner is pretty bare by design. It 
just runs unittest blocks in serial as functions where assert() 
failures are not undefined behavior. Assert messages are not very 
helpful, though the recently added flag `-checkaction=context` 
helps a lot.

Luckily there is a trait for getting unittests:
https://dlang.org/spec/traits.html#getUnitTests

So it is possible to make your own unittest runner, as people 
have done:
http://code.dlang.org/packages/silly
http://code.dlang.org/packages/unit-threaded
http://code.dlang.org/packages/dunit

On Saturday, 3 August 2019 at 13:31:02 UTC, PV wrote:
 Is it possible to somehow customize syntax and running of unit 
 tests?
You can add ("attributes") to your unittests. The default test-runner will not do anything with them currently, but your own test runner can.
 I'd looked into the documentation, found nothing, but perhaps I 
 just missed it. If it is not possible, is it realistic to (very 
 easily) hack the D compiler to implement such feature?
Shouldn't be necessary, I think what you want can be done with attributes and your own test runner.
 Would such feature also work in betterC?
unittest blocks itself are supported in betterC, so you can probably make it work.
Aug 03 2019