Unit Testing Maui with Esri 100.14.0.Preview2 with Xunit

1299
10
06-30-2022 02:38 PM
MeridithSpellerberg
New Contributor

Hi - we are updating an older Xamarin application (UWP, iOS) to Maui (Win UI/iOS) with the latest preview of ESRI. Everything converted fine with the exception of our unit tests. When we create a basic Maui application we are able to add Xunit tests without problem, but when we add in the latest ESRI packages we get the error 

Project '..{our app name}.csproj' targets 'net6.0;net6.0-windows10.0.194941.0.' it cannot be referenced by a project that targets '.NetCoreApp,version=v6.0'.

NU1201 Project {our app name} is not compatible with net6.0 (.NETCoreApp, Version=v6.0).  

 

Has anyone had any luck with this? 

Tags (1)
0 Kudos
10 Replies
dotMorten_esri
Esri Notable Contributor

The net6.0 target framework moniker (tfm) is only supported for class libraries. For unit tests you need to specify a platform specific tfm to run them on, or we have no way to deploy the required native runtime libraries for the correct OS.

0 Kudos
ChrisHu
New Contributor

Hi ESRI,

We also have the same issue. It doesn't allow us to add Unit Test to our .NET MAUI Project due to this error: 'Esri.ArcGISRuntime' nuget package cannot be used to target 'net6.0' ... Only platform-agnostic class libraries or Windows, iOS and Android platforms are supported.

We followed Microsoft's recommended UnitTest tutorial here: https://youtu.be/C9vIDLQwc7M
The problem is that we have to add TargetFramework: net6.to .NET MAUI project in order to add it as a dependency to UnitTest project which targets net6.0.

It means we cannot add Unit Tests as long as we add Esri.ArcGISRuntime.Maui to our .Net MAUI project. This seems to a big red flag for Esri.ArcGISRuntime.Maui. Since Unit Test is essential to any project.

Can ESRI provide a solution to this? 

Thanks,

Chris

0 Kudos
dotMorten_esri
Esri Notable Contributor

The issue here is that ArcGIS Runtime is mainly a native library, and deploys runtimes for each OS it runs on. It's not possible to run that in a platform-agnostic way. It _must_ declare a target runtime in order to run. The good news though is that even unit tests runs on a specific OS with a specific runtime in the end.

There really shouldn't be an issue with you adding a target OS to your unit tests. If you for instance run your unit tests on Windows, just add the '-windows10.0.19041' to your target framework for your unit test project. In the video you linked to, the main project he's testing also have the OS declared in the targets, and there's nothing wrong with also doing this with the unit test project itself.

 

From the video you posted, it's even pointed out the limited set of platforms you can run on:

dotMorten_esri_0-1670350823064.png

 

0 Kudos
ChrisHu
New Contributor

addingTargetFramework "net6.0-ios" to unit test project doesn't compile due to serveral errors. One reason is that  MAUI project produces Exe instead of Dll for TargetFramework "net6.0-ios". You can't add Maui project as an project dependency to your unit test project.  Second reason is the unit test project throws compiler errors once you add "net6.0-ios", now it want you to convert your unit test project to be an iOS project by adding bundleID and other stuff. Could you please provide a sample unit test project for .net MAUI with ESRI.ArcGISRuntime.Maui installed? I believe many people have the same problem when using ESRI.ArcGISRuntime on .Net MAUI platform. Thanks

0 Kudos
dotMorten_esri
Esri Notable Contributor

addingTargetFramework "net6.0-ios" to unit test project doesn't compile 

The Visual Studio Test Explorer runs its tests on Windows, so you'll need to target Windows.
In order to run unit test on an iOS device, you need an actual application (as opposed to a class library) that knows how to discover and run tests inside that application project. Depending on which test framework you use, some of them might provide some APIs to help you do that (we ended up writing our own because we didn't find any that really worked well for our setup).

0 Kudos
FelicityRhone
New Contributor III

I'm trying to get some unit tests going for a project using the 200.0-beta version of the Runtime. I tried this suggestion of targeting the net6.0-windows10.0.19041.0 framework in the test project and it builds, but unfortunately I am running into the issue described here when attempting to run the tests: https://github.com/dotnet/maui/issues/11575

If anyone has a suggestion to work around this it would be greatly appreciated.

0 Kudos
dotMorten_esri
Esri Notable Contributor

Yeah the problem you hit with Maui is it wants to build a packaged application, not a class library. Running unit tests against packaged applications is a lot trickier, and doesn't work well from within Visual Studio - I've only been able to do this from command line with some scripts (pretty much identical to how you'd do it with UWP from commandline).

 

.NET MAUI just isn't really in a great state today for unit testing, and we had to rely on a lot of home-built tooling to do it. My recommendation (for now at least) is to build a class library with your business logic that doesn't take a dependency on Maui itself (for most business logic you shouldn't have to), and run normal unit tests against that.

0 Kudos
dotMorten_esri
Esri Notable Contributor

Attached is an XUnit project that should work on Windows and Mac (didn't get a chance to run it on Mac, so might need a tweak or two)

dotMorten_esri_1-1670353594666.png

I did have an issue with setting <UseMaui>true</UseMaui>, but I don't think that's really necessary since we're bringing that dependency in implicitly via the project reference.

0 Kudos
ChrisHu
New Contributor

First of all, thank you for sharing this test project. I really appreciate your work!

There are two main issues here:

1. I ran the unit test on both Windows and Mac .

It throws this compiler error on Mac: /usr/local/share/dotnet/packs/Microsoft.MacCatalyst.Sdk/15.4.471/tools/msbuild/iOS/Xamarin.Shared.targets(3,3): Error: A bundle identifier is required. Either add an 'ApplicationId' property in the project file, or add a 'CFBundleIdentifier' entry in the project's Info.plist file. (TestProject1)

This is an issue I ran into before, as I mentioned in my previous comment.

2. Your unit test project works on Windows only when <UseMaui>true</UseMaui> is disabled. As you mentioned it in your comment that it breaks test run. It means we couldn't unit test any Maui API or Custom classes requires Maui API. It renders the unit test useless for a .Net Maui application. 

0 Kudos