Reader small image

You're reading from  .NET MAUI Cross-Platform Application Development - Second Edition

Product typeBook
Published inMar 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781835080597
Edition2nd Edition
Languages
Right arrow
Author (1)
Roger Ye
Roger Ye
author image
Roger Ye

Roger Ye is an embedded system programmer who has great interest in virtualization, Android, and cross-platform technologies. His professional experience includes working with major companies like Motorola, Emerson, Intel, and EPAM, where he held the position of Engineering Manager. At Motorola and Emerson, he worked on embedded system projects for mobile devices and telecommunication infrastructures. He is now an engineering manager at EPAM, working with a team to deliver digital solutions for diverse clients.
Read more about Roger Ye

Right arrow

Developing Unit Tests

Testing plays a crucial role in ensuring software quality in contemporary software development. Various types of testing are involved in the software development lifecycle, including unit testing, integration testing, and system testing. Unit testing is employed to examine software modules or components within an isolated environment and is typically carried out by developers. With a well-devised unit test strategy, programming issues can be detected at the earliest stage of the software development lifecycle, rendering unit testing the most efficient and cost-effective approach to guaranteeing software quality.

In .NET MAUI app development, we can utilize existing unit test frameworks or libraries present in the .NET ecosystem. By employing a test framework or library, we can expedite unit test development. An effective test framework is generally designed for seamless integration with a continuous integration (CI) and continuous deployment (CD) environment...

Technical requirements

To test and debug the source code from this chapter, you need to have a .NET 8 environment set up. You can install .NET 8 by following the instructions provided at the Microsoft link below on Windows, macOS, or Linux: https://dotnet.microsoft.com/en-us/download/dotnet/8.0.

Unit tests can be executed from the command line using the dotnet command, or via the Test Explorer in Visual Studio if you’re using Windows.

To set up Visual Studio 2022, please refer to the Development environment setup section in Chapter 1, Getting Started with .NET MAUI, for the details.

The source code for this chapter is available in the following GitHub repository: https://github.com/PacktPublishing/.NET-MAUI-Cross-Platform-Application-Development-Second-edition/tree/main/2nd/chapter11.

To check out the source code of this chapter, we can use the below command:

$ git clone -b 2nd/chapter11 https://github.com/PacktPublishing/.NET-MAUI-Cross-Platform-Application...

Unit testing in .NET

To develop unit test cases, it is common to utilize a unit test framework to enhance efficiency. There are several unit test frameworks available in the .NET environment, which are as follows:

  • Microsoft Test Framework (MSTest) comes bundled with Visual Studio. The initial version of MSTest (V1) was not an open-source product, and it first appeared alongside the release of Visual Studio 2005. For more information about MSTest (V1), please refer to the book Microsoft Visual Studio 2005 Unleashed by Lars Powers and Mike Snell. Later, Microsoft made the MSTest (V2) open-source, hosting it on GitHub, with the first release becoming available around 2017.
  • NUnit is an open-source testing framework ported from JUnit. It was the inaugural unit test framework for .NET, with its earliest releases hosted on SourceForge in 2004. For more information on version 2.0, please refer to the release notes in the Further reading section. The latest releases have since...

Mock .NET MAUI components

In developing unit tests for .NET MAUI, we will present test case development for both XAML-based and Blazor-based apps. In both instances, we will incorporate the MVVM pattern in the design. The unit test cases at the model layer are identical for both; however, testing the view and the view model differs significantly. Developing unit test cases for the view and the view model in a XAML-based app can be quite complex. To test the view model, it is necessary to resolve the dependencies of XAML components. For instance, in the XAML version of our app, we need to invoke Shell navigation methods within the view model, as demonstrated in the following code:

await Shell.Current.GoToAsync(
  $"{nameof(ItemsPage)}?{nameof(ItemsViewModel.ItemId)}={item
  .Id}");

To address dependencies in Xamarin.Forms, an open-source project, Xamarin.Forms.Mocks, is available to help mock Xamarin.Forms components. In the case of .NET MAUI XAML apps, we require...

Razor component testing using bUnit

In .NET MAUI development, we cannot create unit test cases for all view and view model layers in XAML-based UI components; however, this is possible with Blazor. bUnit is an exceptional test library that can be employed for developing unit tests for Razor components. The bUnit library facilitates the creation of unit test cases for Razor components using xUnit, NUnit, or MSTest. Throughout the remainder of this chapter, we will utilize xUnit in conjunction with bUnit. The structure of unit test cases using bUnit bears similarity to the xUnit test cases we discussed in the previous section.

In the remainder of this chapter, we will focus on testing the following Razor components, which were created in the second part of this book:

  • Razor components in the PassXYZ.BlazorUI project
  • Razor components in the PassXYZ.Vault project

To test Razor components using bUnit, it is necessary to modify the project configuration of PassXYZ...

Summary

In this chapter, we discussed unit test development for .NET MAUI apps. Although multiple test frameworks are available, we selected xUnit as our framework for this chapter. In the MVVM pattern, unit testing of the model layer is consistent with any other .NET application. We developed test cases for the IDataStore interface to evaluate our model layer. For unit testing the view and view model, we concentrated on the Blazor Hybrid app utilizing the bUnit test library. By combining the xUnit framework and bUnit library, we can develop end-to-end unit tests for a Blazor Hybrid app. With bUnit, we addressed topics such as Razor templates, the RenderFragment delegate, dependency injection, and the Moq framework.

With the understanding of unit testing acquired in this chapter, you should now be capable of developing your own unit tests. For more information on .NET unit test development, please refer to the Further reading section.

Unit testing can be integrated into a...

Further reading

Learn more on Discord

Join our community’s Discord space for discussions with the author and other readers:

https://packt.link/cross-platform-app

lock icon
The rest of the chapter is locked
You have been reading a chapter from
.NET MAUI Cross-Platform Application Development - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781835080597
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Roger Ye

Roger Ye is an embedded system programmer who has great interest in virtualization, Android, and cross-platform technologies. His professional experience includes working with major companies like Motorola, Emerson, Intel, and EPAM, where he held the position of Engineering Manager. At Motorola and Emerson, he worked on embedded system projects for mobile devices and telecommunication infrastructures. He is now an engineering manager at EPAM, working with a team to deliver digital solutions for diverse clients.
Read more about Roger Ye