At the moment, I have created a test project using a project template from Visual Studio Unit Test App (Winui 3) and my code looks like this
namespace TestMyWinrtApp
{
TEST_CLASS(CppUnitTests)
{
public:
TEST_METHOD(CppTestOne)
{
try {
auto bp = winrt::make< winrt::MyUI::implementation::BlankPage>();
Button btn = bp.FindName(L"myButton").as<winrt::Microsoft::UI::Xaml::Controls::Button>();
winrt::hstring btnTag = btn.Tag().as<winrt::hstring>();
Assert::IsTrue(btnTag == L"MyTag");
} catch (const winrt::hresult_error& ex) {
MessageBox(
NULL,
ex.message().c_str(),
L"Error",
MB_OK | MB_ICONINFORMATION
);
}
}
};
}
However, after starting the tests, I get the exception: "The Application Called An Interface that was Marshalled for a Different Thread". As far as I know, this exception thrown is due to the fact that I try to create an winrt::make< winrt::MyUI::implementation::BlankPage>() UI element outside the UI thread. I found a solution for C# which is to add an [UITestMethod] attribute for the test method. But how i can do unit tests like this in C++ test project?
[UITestMethod]as well and easy as with c#.