I am building a WinUI 3 app in C#, .Net 6.0, VS 2022 and I would like to add Unit Tests using MsTest but I get errors when I try to reference back to the project that I want to test. Is it possible to use MsTest in this scenario? Any suggestions how to get it to work?
The closest thing to instructions I could find is this blog entry.
It appears that you can't add an MsTest project to your solution, rather you have to add another shell of a WinUI 3 project and add nugets and config to make it a integrate with the VS Unit Test framework. The instructions date back to early WinUI 3 development and some steps no longer appear to be relevant (such as consolidating csproj and wapproj).
The instructions seem to work up to a point - I can create basic unit tests that appear in the Test Explorer window in VS. The problem occurs when I try to create a reference from my test project back to the project that I want to test. As soon as I add the reference in the unit test project, I get the following errors when I try to build (or run tests) in my unit test app:
Severity Code Description Project File Line Suppression State
Error PRI277: 0x80073b0f - Conflicting values for resource 'Files/MainWindow.xbf' UnitTestProject D:\source\repos\MyApp\UnitTestProject\WINAPPSDKGENERATEPROJECTPRIFILE 1
Error PRI175: 0x80073b0f - Processing Resources failed with error: Duplicate Entry. UnitTestProject D:\source\repos\MyApp\UnitTestProject\WINAPPSDKGENERATEPROJECTPRIFILE 1
I am not sure where these come from or how to try to debug.
I tried removing the default asset png files from my Unit test project to see if they were related to the problem and the errors changed to:
Severity Code Description Project File Line Suppression State
Error PRI175: 0x80073b0f - Processing Resources failed with error: Duplicate Entry. UnitTestProject D:\source\repos\MyApp\UnitTestProject\WINAPPSDKGENERATEPROJECTPRIFILE 1
Error PRI277: 0x80073b0f - Conflicting values for resource 'Files/App.xbf' UnitTestProject D:\source\repos\MyApp\UnitTestProject\WINAPPSDKGENERATEPROJECTPRIFILE 1
Now complaining about App.xbf
instead of MainWindow.xbf
The project file for my unit test project is listed below. I have made sure that WindowsAppSDK and BuildTools packages are the same version as in the project I want to test. It does not appear to be possible to use the versions of the test environment packages suggested in the blog instructions since they are now depreciated.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>UnitTestProject</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
<WindowsPackageType>None</WindowsPackageType>
</PropertyGroup>
<ItemGroup>
<ProjectCapability Include="TestContainer" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="17.8.0">
<ExcludeAssets>build</ExcludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="8.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231115000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
<!--
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored.
-->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<Folder Include="Assets\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyApp\MyApp.csproj" />
</ItemGroup>
<!--
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
Explorer "Package and Publish" context menu entry to be enabled for this project even if
the Windows App SDK Nuget package has not yet been restored.
-->
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>
</Project>
Apparently what I was trying does not work. There is an issue raised in the WindowsAppSDK project but it has not been resolved.
Unit Test does not work for Windows App SDK 1.2 #3245
The only work around appears to be to add Unit Tests within the same project as the code you want to test. If you try to create a separate project for the unit tests then it gives the errors when you try to add a dependency to the original project.