[try Beta version]
Not logged in

 
Is it possible to use catch2 testing in a graphics windows application?

Jan 13, 2024 at 5:18pm
Hi,

I have a Windows graphics application built with MFC and wanted to write unit tests using the Catch2 framework but when it executes it runs the dialog and it does not run the test. See a bit of my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class CGridCtrlTest :   public CGridCtrl
{
public:
	int* p = nullptr;
	int* get() const { return p; }
};


TEST_CASE_METHOD(CGridCtrlTest, "Test method")
{
	p = new int{ 44 };
	auto x = get();
        REQUIRE(p != nullptr);
}

TEST_CASE("test function")
{
	REQUIRE(1 == 1);
}



Any ideas?

Juan
Jan 13, 2024 at 6:10pm
If only you did a bit of net searching before you ask your questions....

https://duckduckgo.com/?t=ffab&q=use+catch2+in+windows+gui+apps&ia=web

Yes, it is possible. You need to make the effort to doing the work.
Jan 15, 2024 at 10:22am
After searching the web I tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <catch2/catch_all.hpp>
#include <Windows.h>




extern "C" int WINAPI
wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
	int result = Catch::Session().run(__argc, __wargv);
	return result;
}


TEST_CASE("test function")
{
	REQUIRE(1 == 1);
}



and when run, the solution opens a dialog saying "Root element missing"
and the title "Error while running unit tests"

I don't find what next to do..

Last edited on Jan 15, 2024 at 10:30am
Jan 15, 2024 at 12:28pm
After much study, I come to the conclusion that although you can start a GUI application to work with Catch2 by providing this function:

1
2
3
4
5
6
7
8
9
extern "C" int WINAPI
wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
	if (AttachConsole(ATTACH_PARENT_PROCESS)) {
		freopen("CONOUT$", "wb", stdout);
		freopen("CONOUT$", "wb", stderr);
	}
	int result = Catch::Session().run(__argc, __wargv);
	return result;
}


the GUI code cannot be tested because the dialogs are not created and the GUI foundation is not executed....

so you may write TEST_CASE()s but not about MFC code!

So, how can one test a dialog box??


CATCH2 does not allow us to test MFC code!

// _WINDOWS;CATCH_CONFIG_WCHAR;_WIN32;UNICODE
Topic archived. No new replies allowed.