Testing .NET MAUI Application using Appium and xUnit

Testing .NET MAUI Application using Appium and xUnit

29 February 2024

.NET MAUI/Xamarin

Buy Me A Coffee

Hello! 👋

As we delve into the realm of unit testing and integration testing for .NET MAUI applications on this unique February 29th, 2024, it's essential to acknowledge the significance of ensuring the robustness and reliability of our code.

In this article, we'll explore the fundamentals of unit testing and integration testing for .NET MAUI applications, providing practical insights and examples to guide you in setting up and writing tests effectively. Whether you're familiar with test-driven development or looking to enhance your testing practices, let's embark on this journey of ensuring the quality and resilience of your .NET MAUI projects.

Now, let's continue with the main content, incorporating examples of setting up and writing tests for .NET MAUI applications.

Unit Testing in .NET MAUI

In .NET MAUI development, unit testing plays a crucial role in ensuring the reliability and functionality of individual units of code. As a .NET software engineer using C#, you are likely familiar with xUnit, a popular unit testing framework. Here are awesome videos demonstrating step-by-step guides to set and write unit tests for your application:

YouTube Video Link

YouTube Video Link

UI Testing in .NET MAUI

UI testing is crucial for ensuring that various parts of your .NET MAUI application work seamlessly together. As we step into the realm of mobile application testing, one tool that stands out for its versatility and effectiveness is Appium.

Appium is an open-source and cross-platform mobile application automation tool that supports all .NET MAUI platforms. What sets Appium apart is its commitment to providing a single automation API that works across different mobile platforms, making it a preferred choice for those seeking a unified approach to mobile testing.

Install Appium and drivers for each platform:

For UI integration tests using Appium, create a new xUnit project for each platform.

For Android, your csproj may look like this:

The next step is preparing the AppiumSetup for each platform.

I try to make the test as much independent from user interaction, so it can be executed on CI.

Android AppiumSetup class creates an Android Emulator and installs the APK file on it.

For all other platforms the AppiumSetup classes can be found on GitHub.

Pay attention to Windows setup. You need to start the application manually or use process = Process.Start("explorer.exe", "YOUR-APP-IDENTIFIER:");, Appium doesn't start WinUI application automatically.

That's it with setting platform-specific code. The next part is Shared for all platforms.

Create a folder "Client.Shared.UITests". This is where we will store our tests and helper classes.

Create an AppiumServiceHelper class. It creates and executes AppiumLocalService.

Create a base class for all tests:

With UI testing, we compare the expected snapshot with the actual image we received during test execution. The BaseTest class has methods, that allow comparison of the snapshots.

So many preparations. When I will finally write the test?

Here are the tests:

Here we have 2 simple tests. The first one just waits 2 seconds and then takes the screenshot and compares it with the predefined "AppLaunches.png". The second test finds and clicks on the Login button and again compares the screenshots.

But what if I want to run some platform tests only on a specific runtime? Let's leave it for the next article.

Conclusion

As a .NET software engineer, incorporating both unit testing and UI testing into your development workflow will contribute to the overall quality and reliability of your .NET MAUI applications.

Happy coding, and may your .NET MAUI projects thrive with the power of comprehensive testing.

Buy Me A Coffee

Related:

Creating a card stack layout using .NET MAUI

The article describes how to create a custom control for stacking cards.

Adding SQLite to the .NET MAUI application

Connect to SQLite from .NET MAUI application.

An unhandled error has occurred. Reload

🗙