How can one reliably run unittests

Steven Schveighoffer schveiguy at gmail.com
Fri Aug 27 14:18:48 UTC 2021


On 8/27/21 9:35 AM, Dennis wrote:
> On Friday, 27 August 2021 at 10:30:58 UTC, deadalnix wrote:
>> This is the wrong question. When one runs the unittests, they don't 
>> care about main. The existance of the main flag is dubious to begin with.
> 
> Saying "-unittest should have been designed differently" doesn't help us 
> forward. Currently it's possible to have a -betterC test runner in main, 
> or set `UnitTestResult.runmain = true` from `core.runtime`, or to create 
> a unittest build with incremental separate compilation, so changing the 
> semantics of the -unittest flag is a very disruptive change.
> 
> However, like you said, there is no use case for having two `main` 
> functions in one build, so I was looking into improving that (see 
> https://github.com/dlang/dmd/pull/13026), which is how I got to that 
> very real implementation question.
> 
> jfondren pointed out that the default test runner doesn't run main, so I 
> think it's best to make -main only insert main when there isn't already 
> one being compiled in. Then you should be able to do `dmd -unittest 
> -main -run somemodule.d` on any (self-contained) d file with consistent 
> results.

The default test runner doesn't run main *by default*, but it also *can* 
run main if passed a runtime option. So it still needs a main to run if 
requested.

I think a "-nomain" option is something worth looking at (basically, the 
generated `extern(C) main` function doesn't call a missing `dmain` 
function). It can't be tied to `-unittest` though.

-Steve


More information about the Digitalmars-d mailing list