Targeting Specific Runtimes for Platform Tests

Targeting Specific Runtimes for Platform Tests

21 March 2024

.NET MAUI/Xamarin

Buy Me A Coffee

Hello! 👋

In the previous article, we discussed how to set up and run tests for your .NET MAUI application. The tests are shared for all platforms, but what if you need to test your application against a specific runtime version or environment? Here's how to achieve targeted testing for your platform interactions.

Step 1. Define Runtime and platform

Create a class RuntimePlatform:

Here we define constants for all pairs of Runtime and Platform we need for tests. For example, we want to run Android app tests on Windows and OSX, but Tizen only on OSX.

Step 2. Create an attribute

For tests, I use xUnit. To define the test we need to set the Fact attribute for the test method. But as we want to skip the test on some platforms, we need to extend the Fact attribute.

Create a new class AllowOnPlatformFactAttribute:

This attribute checks if the current runtime and platform match the defined attribute parameter. If the condition is false, the test is skipped.

Step 3. Apply the attribute

The final step is to replace Fact attribute with AllowOnPlatformFact attribute, so our launch test looks like this:

Conclusion

By implementing targeted platform tests, you gain several advantages:

  • Increased Confidence: You can ensure your application functions correctly across various runtime environments, boosting overall confidence in its robustness.

  • Improved Efficiency: By focusing tests on specific runtimes, you can reduce overall test execution time, leading to faster development cycles.

  • Simplified Maintenance: Documented runtime configurations make it easier to maintain and update your test suite as your application and supported platforms evolve.

Happy testing!

Buy Me A Coffee

Related:

The first project with .NET MAUI

How to migrate Xamarin.Forms app to .NET MAUI step by step.

Prepare .NET MAUI application with CommunityToolkit.MAUI to release

The article describes how to configure .NET MAUI CommunityToolkit to avoid crashes at runtime.

An unhandled error has occurred. Reload

🗙