Thread overview
Unittest in a windows app
Dec 19, 2014
Dan Nestor
Dec 20, 2014
Dan Nestor
Dec 20, 2014
Dicebot
Dec 20, 2014
Rainer Schuetze
December 19, 2014
Hello everybody, this is my first post on this forum.

I have a question about unit testing a Windows application. I
have slightly modified Visual D's default Windows application
stub to the following:

<code>
module winmain;

import core.runtime;
import core.sys.windows.windows;

unittest {
	assert(false);
}

extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow)
{
	int result;

	void exceptionHandler(Throwable e)
	{
		throw e;
	}

	try
	{
		Runtime.initialize();
		result = myWinMain(hInstance, hPrevInstance, lpCmdLine,
nCmdShow);
		Runtime.terminate();
	}
	catch (Throwable o)		// catch any uncaught exceptions
	{
		MessageBoxA(null, cast(char *)o.toString(), "Error", MB_OK |
MB_ICONEXCLAMATION);
		result = 0;		// failed
	}

	return result;
}

int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow)
{
	/* ... insert user code here ... */
	throw new Exception("not implemented");
	return 0;
}
</code>

I compiled it with the `-unittest` option. Strangely, when
running the app, no error is displayed, and the application
proceeds as usual. I would expect the program to display the unit
test failure and stop (the behaviour I have observed for console
applications). What am I missing?

Thanks for helping!

Dan
December 20, 2014
I managed to isolate the problem to the following. Program 1 below works (displays unit test failure when run), while program 2 does not.

***** Program 1 *****

import std.stdio;

unittest
{
    assert(false);
}

void main()
{
    writeln("Hello D-World!");
}

***** Program 2 *****

module winmain;

import core.sys.windows.windows;

unittest {
    assert(false);
}

extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow)
{
    return 0;
}
December 20, 2014
Can it be because Windows main wrapper consumes exceptions or spawn a separate thread for that or something like that?
December 20, 2014

On 19.12.2014 22:39, Dan Nestor wrote:
> Hello everybody, this is my first post on this forum.
>
> I have a question about unit testing a Windows application. I
> have slightly modified Visual D's default Windows application
> stub to the following:
>
[...]
>      try
>      {
>          Runtime.initialize();

Runtime.initialize() no longer calls the unittests since a few versions. You have to call runModuleUnitTests() explicitely, e.g.

      try
      {
          Runtime.initialize();
          if (runModuleUnittests())
		result = myWinMain(hInstance, hPrevInstance, lpCmdLine,
			           nCmdShow);
          Runtime.terminate();
      }...