Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
50
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 12 Testing

because it forces you to expose these small units of functionality and ultimately follow SOLID principles.

Unit testing itself will not catch all bugs in the system and should not be relied upon as a sole means of testing your applications. When used in combination with other forms of testing such as integration, functional, or end-to-end testing, you can build up confidence that your application is stable and delivers what is required.

Let’s see how to implement unit testing with .NET MAUI.

Unit Testing in .NET MAUI

.NET MAUI applications are, as the name suggests, .NET-based projects, meaning that any of the existing .NET-based unit testing frameworks can be used.

As it currently stands, the default .NET MAUI project is not compatible with a unit test project. I will cover how to solve this in the “Adding a Unit Test Project to Your Solution” section.

There are three well-known frameworks that come with template support in Visual Studio, meaning you can create them with File Add New Project option. The three frameworks are listed below.

xUnit

xUnit appears to be the choice of the .NET MAUI team. One main reason for this is likely the support around being able to run xUnit-based unit tests on actual devices, meaning you can test device-specific implementations.

https://xunit.net

366

Chapter 12 Testing

NUnit

NUnit is an old favorite of mine. I have used it on so many projects in the past! It has some great features like being able to run the same test case with multiple sets of data to reduce the amount of testing code you need to write and ultimately maintain.

https://nunit.org

MSTest

MSTest is a testing framework that is built and supplied by Microsoft. It doesn’t appear as feature rich as NUnit or xUnit but it still does a great job.

https://learn.microsoft.com/dotnet/core/testing/unit- testing-with-mstest

Your Chosen Testing Framework

We will be using xUnit for this book mainly due to the benefits it brings with being able to also run the unit tests on devices.

Tests in xUnit are decorated with the [Fact] attribute with the expectation that as the author of the test methods you will name them in a way that defines a fact which the test will prove to be true.

Most of the test frameworks are quite similar and tend to differ in terms of keywords when identifying tests. Go with whatever testing framework you are most comfortable with. If you do not have much experience

with any, perhaps experiment with each to see which gives you the best experience. At the end of the day, you will be building and maintaining these tests so it needs to benefit you and your team.

367