How to write unit tests with Maps SDK and MAUI

737
5
10-23-2023 09:49 AM
Labels (4)
jsiemensLatitudegeo
New Contributor

Hi,

I've tried several methods of being able to write unit tests with MAUI and the Maps SDK but none have worked. I tried the approach described here:

https://www.youtube.com/watch?v=C9vIDLQwc7M

and here: https://cedricgabrang.medium.com/adding-xunit-test-to-your-net-maui-project-ee36c00a8542

This is with a brand new MSTest project template from VS. csproj looks like this:

 

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Esri.ArcGISRuntime" Version="200.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

</Project>

 

This works fine without Maps SDK, but as soon as I add the Esri.ArcGISRuntime nuget package reference, I get this:

Severity Code Description Project File Line Suppression State
Error The 'Esri.ArcGISRuntime' nuget package cannot be used to target 'net7.0' for output type 'Exe'. Only platform-agnostic class libraries or Windows, iOS and Android platforms are supported. TestProject1 C:\Users\JSIEMENS\.nuget\packages\esri.arcgisruntime\200.2.0\buildTransitive\net6.0\Esri.ArcGISRuntime.targets 5

So then I saw a suggestion to set the TargetFramework to "net7.0-windows10.0.19041.0", so I try that, and it works fine without Maui, but once I add <UseMaui>true</UseMaui>, then I get:

Architecture: x64
Framework: 'Microsoft.Maui.Core', version '**FromWorkload**' (x64)
.NET location: C:\Program Files\dotnet\
No frameworks were found.
Learn about framework resolution:
https://aka.ms/dotnet/app-launch-failed
To install missing framework, download:
https://aka.ms/dotnet-core-applaunch?framework=Microsoft.Maui.Core&framework_version=**FromWorkload*...
. Please check the diagnostic logs for more information.

It feels the same as this issue:

https://github.com/dotnet/maui/issues/17914

And: https://github.com/dotnet/maui/issues/11575

Here's the csproj again.

 

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
	<UseMaui>true</UseMaui>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
    <PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
    <PackageReference Include="coverlet.collector" Version="3.2.0" />
  </ItemGroup>

</Project>

 

So at this point in time, it feels like there is no way to run unit tests against a project that uses both MAUI and the ArcGIS Maps SDK. What am I missing? Is there a way to get tests to work?

Formerly with Xamarin Forms and UWP, there was a UWP test project template (which was horrifically slow to run tests with), but that doesn't seem to be a thing anymore with WinUI3.

0 Kudos
5 Replies
dotMorten_esri
Esri Notable Contributor

> So at this point in time, it feels like there is no way to run unit tests against a project that uses both MAUI and the ArcGIS Maps SDK. 

The Maps SDK only ships runtimes for Windows, iOS, Catalyst and Android. You _must_ run the unit tests in the context of these platforms, which means executing the tests in the context of an application (similar to how UWP did, but you can probably get your Windows build run un-packaged to run from commandline - see doc on running unpackaged WinUI apps). When you add the UseMaui bit, I believe it defaults to running the apps as packaged - the command line for executing tests in packaged Windows apps is similar to how you'd run UWP unit tests.

We run unit tests daily with maui, and we basically launch an runner-app that runs the tests and reports the results back. This is mostly a home-grown system, since Microsoft still hasn't provided a good OOTB solution for executing unit tests on devices and inside WinUI packaged apps.

0 Kudos
jsiemensLatitudegeo
New Contributor

This puts us in a very difficult position. I'm not really sure how to go about creating a runner to the run the tests. I see there are some approaches that use a 'visual runner' that's effectively just an app that runs the tests. But I'm not sure how I can integrate that with the command line to run on a CI build server, and I fear the development workflow for running those tests would be horrifically slow and cumbersome. 

With Xamarin Forms and ArcGIS Runtime 100.x we were able to run the tests using .net 6 using the test explorer and easily integrate into the build pipeline using dotnet test.

At this point, I'm investigating the idea of splitting up our code base to factor out the Esri bits and the UI bits so that I can create a net7 build output to unit test everything that isn't Esri, and a separate project that has all the Esri stuff and no UI so I can unit test that with net7.0-windows10.0.19041.0.

0 Kudos
dotMorten_esri
Esri Notable Contributor

> With Xamarin Forms and ArcGIS Runtime 100.x we were able to run the tests using .net 6 using the test explorer. 

Xamarin.Forms had the same limitation. For .NET 6 it is similar that you need to target net6-windows even with 100.15. That hasn't changed with 200.x. 

We've frequently asked Microsoft to improve their test framework to better support both WinUI, iOS and Android, because we realize it is limited, and we don't want to maintain our custom test runners any more than you do.

0 Kudos
jsiemensLatitudegeo
New Contributor

With Runtime 100.x and .net 6, we could add the reference to the ArcGIS Runtime, but if we loaded a class that referenced any of the Runtime 100.x UI (eg. MapView) then it would throw an exception, so we developed an interface around MapView, and used the interface instead of MapView. We could run the real Esri MapView at application runtime and a test implementation of MapView during unit tests.

With 200.x, we can't even install the package.

0 Kudos
jsiemensLatitudegeo
New Contributor

For example, previously I could do this inside a .net6 unit test app and it worked.

 

 

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public async Task TestMethod1()
        {
            var portal = await ArcGISPortal.CreateAsync();
        }
    }

 

 

Now, there's a build target that prevents the app from building. If I remove that check in the build target, I get:

Error: System.BadImageFormatException: Could not load file or assembly 'Esri.ArcGISRuntime, Version=200.2.0.0, Culture=neutral, PublicKeyToken=8fc3cc631e44ad86'. Reference assemblies cannot be loaded for execution. (0x80131058)

 

0 Kudos