

Also from Kynth Studios


Also from Kynth Studios


Also from Kynth Studios
123456# XAML Unit Testing Guidelines78This guide provides conventions for writing XAML unit tests in `src/Controls/tests/Xaml.UnitTests/`.910## 1. File Naming and Location for Issue Tests1112When writing a test for a GitHub issue:1314- **Location**: Place the test file in the `Issues/` folder15- **Naming convention**: `MauiXXXXX.xaml` and `MauiXXXXX.xaml.cs` (where XXXXX is the GitHub issue number)1617**Example:**18```19src/Controls/tests/Xaml.UnitTests/Issues/20├── Maui12345.xaml21└── Maui12345.xaml.cs22```2324## 2. Basic Test Pattern2526Most unit tests will:271. Check that the XAML page can be created/inflated282. Perform some assertions on the created page293. Apply to all `[XamlInflator]` test variations (runtime, XamlC, SourceGen)3031**XAML file (Maui12345.xaml):**32```xaml33<?xml version="1.0" encoding="utf-8" ?>34<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"35 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"36 x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui12345">37 <!-- Your test content -->38</ContentPage>39```4041**Code-behind (Maui12345.xaml.cs):**42```csharp43namespace Microsoft.Maui.Controls.Xaml.UnitTests;4445public partial class Maui12345 : ContentPage46{47 public Maui12345()48 {49 InitializeComponent();50 }5152 [TestFixture]53 class Tests54 {55 [Test]56 public void MyTest([Values] XamlInflator inflator)57 {58 var page = new Maui12345(inflator);59 Assert.That(page, Is.Not.Null);60 // Additional assertions61 }62 }63}64```6566**Note**: Only add `[SetUp]` and `[TearDown]` methods if your test requires special initialization (e.g., setting up `DispatcherProvider` or `Application.Current`). Most tests don't need them.6768## 3. Testing IL Correctness (XamlC) or Source Generation6970When the test specifically validates compile-time behavior for a specific inflator:7172**XamlC (IL generation):**73```csharp74[Test]75public void MyTest([Values] XamlInflator inflator)76{77 if (inflator == XamlInflator.XamlC)78 Assert.Throws<BuildException>(() => MockCompiler.Compile(typeof(Maui12345)));79 else80 {81 var page = new Maui12345(inflator);82 Assert.That(page, Is.Not.Null);83 }84}85```8687**Source Generation:**88```csharp89using static Microsoft.Maui.Controls.Xaml.UnitTests.MockSourceGenerator;9091[Test]92public void MyTest([Values] XamlInflator inflator)93{94 if (inflator == XamlInflator.SourceGen)95 {96 var result = CreateMauiCompilation()97 .WithAdditionalSource(98"""99namespace Microsoft.Maui.Controls.Xaml.UnitTests;100101[XamlProcessing(XamlInflator.Runtime, true)]102public partial class Maui12345 : ContentPage103{104 public Maui12345() => InitializeComponent();105}106""")107 .RunMauiSourceGenerator(typeof(Maui12345));108 Assert.That(result.Diagnostics, Is.Empty); // or Is.Not.Empty for error cases109 }110 else111 {112 var page = new Maui12345(inflator);113 Assert.That(page, Is.Not.Null);114 }115}116```117118## 4. Handling Invalid Code Generation119120When testing scenarios where code generation is **expected to fail** or produce invalid code, use special file extensions to prevent build failures:121122| Extension | Behavior | Use Case |123|-----------|----------|----------|124| `.rt.xaml` | Runtime inflation only | XamlC/SourceGen should fail, but runtime should work |125| `.rtsg.xaml` | Runtime + SourceGen only | XamlC should fail |126| `.rtxc.xaml` | Runtime + XamlC only | SourceGen should fail |127128**Why?** These extensions prevent the compiler and build tasks from processing the XAML file, allowing your test to execute and verify the expected behavior.129130**Example:**131```132Issues/133├── Maui12345.rt.xaml # Only inflated at runtime (compiler/sourcegen skip)134└── Maui12345.xaml.cs135```136137## Quick Reference138139| Scenario | Location | Naming | Special Extension |140|----------|----------|--------|-------------------|141| Issue test | `Issues/` | `MauiXXXXX.*` | None (unless invalid codegen) |142| Invalid XamlC | `Issues/` | `MauiXXXXX.rt.xaml` | `.rt.xaml` |143| Invalid SourceGen | `Issues/` | `MauiXXXXX.rt.xaml` or `.rtxc.xaml` | Depends on what should fail |144| Test XamlC IL | Anywhere | Any | Use `MockCompiler` |145| Test SourceGen | Anywhere | Any | Use `MockSourceGenerator` |146147## Related Documentation148149- `.github/copilot-instructions.md` - General MAUI development guidelines150- `.github/instructions/uitests.instructions.md` - UI testing guidelines (different from XAML unit tests)151
One repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| dotnet/maui.github/instructions/public-api.instructions.md · 23k | Copilot instructions | apido-not | 59/100 | 9 days ago | |
| dotnet/maui.github/copilot-instructions.md · 23k | Copilot instructions | setuptestlint-formatstyle+6 | 76/100 | 14 days ago | |
| dotnet/maui.github/instructions/android.instructions.md · 23k | Copilot instructions | buildstyle | 70/100 | 14 days ago | |
| dotnet/maui.github/instructions/ci-copilot-pipeline-security.instructions.md · 23k | Copilot instructions | gitsecuritydeploymentdo-not+1 | 76/100 | 14 days ago | |
| dotnet/maui.github/instructions/collectionview-android.instructions.md · 23k | Copilot instructions | stylearchperformanceagent-behaviour | 52/100 | 14 days ago | |
| dotnet/maui.github/instructions/collectionview-handler-detection.instructions.md · 23k | Copilot instructions | stylegitdo-not | 73/100 | 14 days ago | |
| dotnet/maui.github/instructions/collectionview-ios.instructions.md · 23k | Copilot instructions | styleperformance | 48/100 | 14 days ago | |
| dotnet/maui.github/instructions/collectionview-windows.instructions.md · 23k | Copilot instructions | stylearch | 52/100 | 14 days ago | |
| dotnet/maui.github/instructions/handler-patterns.instructions.md · 23k | Copilot instructions | styledo-not | 55/100 | 14 days ago | |
| dotnet/maui.github/instructions/helix-device-tests.instructions.md · 23k | Copilot instructions | setupbuildtestarch | 74/100 | 14 days ago | |
| dotnet/maui.github/instructions/integration-tests.instructions.md · 23k | Copilot instructions | setupteststyledo-not | 92/100 | 14 days ago | |
| dotnet/maui.github/instructions/layout-system.instructions.md · 23k | Copilot instructions | archapiperformancedo-not | 55/100 | 14 days ago | |
| dotnet/maui.github/instructions/performance-hotpaths.instructions.md · 23k | Copilot instructions | styleperformancedo-not | 55/100 | 14 days ago | |
| dotnet/maui.github/instructions/sandbox.instructions.md · 23k | Copilot instructions | buildteststyletesting-strategy+4 | 81/100 | 14 days ago | |
| dotnet/maui.github/instructions/templates.instructions.md · 23k | Copilot instructions | buildteststylearch+1 | 92/100 | 14 days ago | |
| dotnet/maui.github/instructions/threading-async.instructions.md · 23k | Copilot instructions | styleui | 48/100 | 14 days ago | |
| dotnet/maui.github/instructions/safe-area-ios.instructions.md · 23k | Copilot instructions | stylegit | 43/100 | 14 days ago | |
| dotnet/maui.github/instructions/uitests.instructions.md · 23k | Copilot instructions | setupbuildteststyle+5 | 79/100 | 14 days ago |
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| dotnet/roslyn.github/instructions/Compiler.instructions.md · 21k | Copilot instructions | buildteststylearch+3 | 99/100 | today | |
| ardalis/CleanArchitecture.github/copilot-instructions.md · 18k | Copilot instructions | buildteststylearch+4 | 96/100 | 14 days ago | |
| dotnet/maui.github/instructions/templates.instructions.md · 23k | Copilot instructions | buildteststylearch+1 | 92/100 | 14 days ago | |
| dotnet/maui.github/instructions/integration-tests.instructions.md · 23k | Copilot instructions | setupteststyledo-not | 92/100 | 14 days ago | |
| dotnet/roslyn.github/copilot-instructions.md · 21k | Copilot instructions | buildteststylearch+3 | 89/100 | 8 days ago | |
| we-promise/sure.github/copilot-instructions.md · 9.5k | Copilot instructions | setuptestlint-formatstyle+10 | 88/100 | 13 days ago | |
| microsoft/WSL.github/copilot-instructions.md · 33k | Copilot instructions | setupbuildtestlint-format+7 | 88/100 | 14 days ago | |
| PowerShell/PowerShell.github/instructions/start-native-execution.instructions.md · 55k | Copilot instructions | buildstylearchgit+1 | 86/100 | 14 days ago |
A badge carrying the measured quality of the strongest agent config file in this repository, out of 100. It reads from this index every time somebody loads your page, so it changes when the measurement changes and there is nothing to keep up to date. Free, no account, and the value is not something you or we can set by hand.
[](https://rulestack.kynth.studio/configs/dotnet-maui-github-instructions-xaml-unittests-instructions)Would rather not hotlink us? Every badge is also served in shields.io’s endpoint schema, so shields renders the image and your readers never talk to our domain:
Published by Toolproof, the masthead over this index and eight others. The method behind the number is at toolproof.kynth.studio/methodology, and the whole thing is readable as JSON with no key at /api.