-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Testing
Framework
- XUnit is used in most of the projects any new test projects created should utilize XUnit.
- NUnit is used in some legacy projects, we tend to migrate all tests to run under XUnit so any new test projects created should utilize XUnit.
- xharness is a dotnet tool and TestRunner library that makes running unit tests in mobile platforms easier.
- Unit tests - These are tests that will not run on a device. This is useful for testing device independent logic.
├── Controls
│ ├── test
│ │ ├── Controls.Core.UnitTests
│ │ ├── Controls.Core.XamlUnitTests
├── Core
│ ├── test
│ │ ├── Core.UnitTests
├── Essentials
│ ├── test
│ │ ├── Essentials.UnitTests
├── Graphics
│ ├── test
│ │ ├── Graphics.Tests
├── SingleProject
│ ├── test
│ │ ├── Resizetizer.UnitTests
- Device tests - These are tests that will run on an actual device or simulator
├── Controls
│ ├── test
│ │ ├── Controls.DeviceTests
├── Core
│ ├── test
│ │ ├── Core.DeviceTests
├── Essentials
│ ├── test
│ │ ├── Essentials.DeviceTests
├── Graphics
│ ├── test
│ │ ├── Graphics.DeviceTests
├── BlazorWebView
│ ├── test
│ │ ├── MauiBlazorWebView.DeviceTests
- UI tests - These are tests that will run on an actual device or simulator and interact with an application
├── Controls
│ ├── test
│ │ ├── Controls.AppiumTests
- Controls.UITests: .NET MAUI Controls Visual Runner for running device based xunit tests. This is useful for tests that require XAML features
25/01/2022 | |||
---|---|---|---|
Controls.Core.UnitTests | 4670 | ||
Controls.Core.XamlUnitTests | 989 | ||
Controls.DeviceTests.ios | 198 | ||
Controls.DeviceTests.android | 214 | ||
Controls.DeviceTests.maccatalyst | 199 | ||
Controls.DeviceTests.windows | 230 | ||
Core.UnitTests | 475 | ||
Core.DeviceTests.ios | 2782 | ||
Core.DeviceTests.android | 599 | ||
Core.DeviceTests.maccatalyst | 592 | ||
Core.DeviceTests.windows | 440 | ||
Essentials.UnitTests | 989 | ||
Essentials.DeviceTests.ios | 280 | ||
Essentials.DeviceTests.android | 278 | ||
Essentials.DeviceTests.maccatalyst | 280 | ||
Essentials.DeviceTests.windows | 287 | ||
MauiBlazorWebView.DeviceTests | 1 | ||
Graphics.Tests | 84 | ||
Graphics.DeviceTests | 3 | ||
Total | 13572 |
We use xunit.runner.visualstudio and nunittestadapter for test discoverability and running the tests. This allows us to view the tests from within Visual Studio via the Test Explorer
tool pane and utilizing vstest.console.exe <AssemblyPath>
from a developer command prompt.
The tests on this project are written using XUnit. Add your class and use BaseTestFixture
as your base class. You can also add a [Category("Layout")]
.
You might need to create a Stub for your test you can add that to the TestClasses
folder
public class CarouselViewTests : BaseTestFixture
{
[Fact]
public void TestPositionChangedCommand()
{
var source = new List<string> { "1", "2", "3" };
var carouselView = new CarouselView
{
ItemsSource = source
};
int countFired = 0;
carouselView.PositionChangedCommand = new Command(() => countFired = countFired + 1;);
Assert.Same(source, carouselView.ItemsSource);
carouselView.Position = 1;
Assert.True(countFired == 1);
}
}
The tests on this project are written using NUnit. To create a new test start by adding a new Xaml ContentPage to the Controls.Core.XamlUnitTests project. In the code behind add your NUnit TestFixture. Make sure to specify a 2nd actor for two TestCase
for True/False
so that the test runs with and without XAMLC enabled. Also take note that the default actor will not be called 50% of the times.
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.AutoMergedResourceDictionaries">
<ContentPage.Resources>
<ResourceDictionary>
<Color x:Key="notpink">Purple</Color>
<Color x:Key="pink">Pink</Color>
<ResourceDictionary Source="AppResources/Colors.xaml" />
</ResourceDictionary>
</ContentPage.Resources>
<Label x:Name="label" TextColor="{StaticResource notpink}" BackgroundColor="{StaticResource Primary}"/>
</ContentPage>
public partial class AutoMergedResourceDictionaries : ContentPage
{
public AutoMergedResourceDictionaries()
{
InitializeComponent();
}
public AutoMergedResourceDictionaries(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}
[TestFixture]
public class Tests
{
[TestCase(false)]
[TestCase(true)]
public void AutoMergedRd(bool useCompiledXaml)
{
var layout = new AutoMergedResourceDictionaries(useCompiledXaml);
Assert.That(layout.label.TextColor, Is.EqualTo(Colors.Purple));
Assert.That(layout.label.BackgroundColor, Is.EqualTo(Color.FromArgb("#FF96F3")));
}
}
}