/avalonia-headless-test:headless-test-setupfrom avalonia-headless-test
Scaffolds headless UI test projects (xUnit + Avalonia.Headless) for Avalonia control libraries. Asks about Skia rendering support and default theme, then generates test projects with sample tests.
Ships in avalonia-headless-test. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Use this skill when a user wants to create a headless test project for an Avalonia control library, or to fix an existing one. The layout and code follow the reference implementation in Ursa.Avalonia/tests.
当用户需要为 Avalonia 控件库创建(或修复)Headless 测试项目时加载此 Skill。 项目结构与代码参考 Ursa.Avalonia/tests。
dotnet test (or by
executing the test assembly directly), with no display server required.Before asking anything, collect these facts (do not guess them):
在提问前先收集以下信息(不要猜测):
| Fact / 信息 | Where to look / 查找位置 |
|---|---|
| Library project(s) to test | src/**/*.csproj referencing Avalonia |
| Avalonia version (11.x or 12.x) | PackageReference Include="Avalonia", or Directory.Packages.props / Directory.Build.props |
| Target framework | <TargetFramework(s)> of the library — test TFM must be compatible (e.g. net8.0, net10.0) |
| Central Package Management | Directory.Packages.props with ManagePackageVersionsCentrally=true |
| Existing tests folder / solution | tests/, *.sln / *.slnx |
| Library-provided theme | A Styles / ResourceDictionary class or .axaml the library ships for its own controls (e.g. Ursa's UrsaSemiTheme) |
Always ask these two questions and wait for the answers before generating files. Present the default in brackets.
生成文件前必须询问以下两个问题并等待回答。
CaptureRenderedFrame() returns null.
不使用 Skia,速度快、依赖少;可测试布局、模板、绑定、输入、焦点、弹层等,
但无法截帧。UseSkia() +
UseHeadlessDrawing = false). Required for tests that inspect pixels,
compare screenshots, or verify a custom Render(DrawingContext) override.
使用 Skia 真实渲染,可通过 CaptureRenderedFrame() 获取像素,用于截图对比或
自定义 Render 的验证。HeadlessTest.<LibName> without Skia for the
fast behavior tests, and RenderTest.<LibName> with Skia for pixel /
screenshot tests. Needed because a test assembly can only have one
[assembly: AvaloniaTestApplication].
同时创建两个项目:不使用 Skia 的 HeadlessTest.<LibName>(行为测试)与使用
Skia 的 RenderTest.<LibName>(像素/截图测试)。因为每个测试程序集只能有一个
AvaloniaTestApplication。Avalonia.Themes.Fluent, <FluentTheme />Semi.Avalonia, <semi:SemiTheme />Also confirm the values inferred in Step 1 (library project, test project
name(s) HeadlessTest.<LibName> / RenderTest.<LibName>, and whether to
include the library's own theme) in the same message, so the user can correct
them at once.
同时在同一条消息中确认步骤 1 推断出的信息(被测项目、测试项目名
HeadlessTest.<LibName> / RenderTest.<LibName>、是否包含库自带主题)。
Replace <LibName> with the library name (e.g. Ursa) and <LibProject> with
the relative path to the library .csproj.
tests/
└── HeadlessTest.<LibName>/
├── HeadlessTest.<LibName>.csproj
├── App.axaml
├── App.axaml.cs
├── TestAppBuilder.cs
├── TestHelpers/
│ └── ControlExtensions.cs
├── Controls/
│ └── <Control>Tests/
│ └── <Control>Tests.cs
└── Rendering/ # only when rendering support = Yes
└── <Control>RenderTests.cs
Group tests per control in Controls/<Control>Tests/ (one folder per control,
namespace HeadlessTest.<LibName>.Controls.<Control>Tests) as Ursa does.
Create two sibling projects. HeadlessTest.<LibName> is exactly the
Rendering = No project (no Rendering/ folder, no Avalonia.Skia).
RenderTest.<LibName> is the Rendering = Yes project with every
HeadlessTest.<LibName> in file names, namespaces and x:Class replaced by
RenderTest.<LibName>; it only holds render tests.
创建两个并列项目:HeadlessTest.<LibName> 即 Rendering = No 的项目;
RenderTest.<LibName> 即 Rendering = Yes 的项目,并把文件名、命名空间与
x:Class 中的 HeadlessTest.<LibName> 全部替换为 RenderTest.<LibName>,只放渲染测试。
tests/
├── HeadlessTest.<LibName>/ # no Skia — behavior tests
│ ├── HeadlessTest.<LibName>.csproj
│ ├── App.axaml / App.axaml.cs
│ ├── TestAppBuilder.cs # "Rendering = No" builder
│ ├── TestHelpers/ControlExtensions.cs
│ └── Controls/<Control>Tests/<Control>Tests.cs
└── RenderTest.<LibName>/ # Skia — pixel / screenshot tests
├── RenderTest.<LibName>.csproj # + Avalonia.Skia
├── App.axaml / App.axaml.cs # same theme as HeadlessTest.<LibName>
├── TestAppBuilder.cs # "Rendering = Yes" builder
└── Rendering/<Control>RenderTests.cs
Pin every Avalonia.* package to the same version the library uses.
| Package | Avalonia 12.x | Avalonia 11.x | When |
|---|---|---|---|
Avalonia.Headless.XUnit | $(AvaloniaVersion) | $(AvaloniaVersion) | always |
xunit.v3 | ≥ the xunit.v3.extensibility.core version required by Avalonia.Headless.XUnit (e.g. 3.2.2 for 12.0.2) | — | Avalonia 12 |
xunit + xunit.runner.visualstudio | — | 2.9.x + 3.1.x | Avalonia 11 |
Microsoft.NET.Test.Sdk | yes | yes | always |
Avalonia.HarfBuzz | $(AvaloniaVersion) | — (package does not exist for 11.x) | Avalonia 12 |
Avalonia.Skia | $(AvaloniaVersion) | $(AvaloniaVersion) | rendering = Yes, or RenderTest.<LibName> when Both |
Avalonia.Themes.Fluent | $(AvaloniaVersion) | $(AvaloniaVersion) | theme = Fluent |
Semi.Avalonia | latest 12.x | latest 11.x | theme = Semi |
Avalonia.Headless.XUnit12.x is built on xunit.v3; 11.x is built on xunit v2 — always use the matching xunit generation. A lowerxunit.v3than the oneAvalonia.Headless.XUnitdepends on producesNU1608warnings.Avalonia.Headless.XUnit12.x 基于 xunit.v3,11.x 基于 xunit v2,不能混用。
HeadlessTest.<LibName>.csproj)<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<OutputType>Exe</OutputType>
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
<UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
<AvaloniaVersion>12.0.2</AvaloniaVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit.v3" Version="3.2.2" />
<PackageReference Include="Avalonia.Headless.XUnit" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.HarfBuzz" Version="$(AvaloniaVersion)" />
<!-- Rendering = Yes, or RenderTest.<LibName> when Both -->
<PackageReference Include="Avalonia.Skia" Version="$(AvaloniaVersion)" />
<!-- Theme: keep exactly one of the two -->
<PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaVersion)" />
<PackageReference Include="Semi.Avalonia" Version="12.0.1" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="<LibProject>" />
</ItemGroup>
</Project>
Set AvaloniaVersion to the library's Avalonia version, or remove the property
if the repository already defines it (e.g. in Directory.Build.props).
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
<PackageReference Include="Avalonia.Headless.XUnit" Version="$(AvaloniaVersion)" />
<!-- no Avalonia.HarfBuzz on 11.x -->
<!-- Avalonia.Skia / theme package exactly as above, with 11.x versions -->
</ItemGroup>
Drop OutputType, TestingPlatformDotnetTestSupport and
UseMicrosoftTestingPlatformRunner for xunit v2.
If the repository has Directory.Packages.props, remove every Version
attribute from the test .csproj and add the versions there instead, e.g.:
若仓库使用中央包管理,从测试 .csproj 去掉 Version,改在
Directory.Packages.props 中声明:
<!--test-->
<PackageVersion Include="Avalonia.HarfBuzz" Version="$(AvaloniaVersion)" />
<PackageVersion Include="Avalonia.Headless.XUnit" Version="$(AvaloniaVersion)" />
<PackageVersion Include="Avalonia.Skia" Version="$(AvaloniaVersion)" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageVersion Include="xunit.v3" Version="3.2.2" />
Skip entries that already exist. Ursa additionally uses
tests/Directory.Build.targets with VersionOverride to run tests against a
different Avalonia version than src/; only add that if the user asks for it.
App.axaml — Fluent<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="HeadlessTest.<LibName>.App"
RequestedThemeVariant="Light">
<Application.Styles>
<FluentTheme />
<!-- library theme, if any, goes after the base theme -->
</Application.Styles>
</Application>
App.axaml — Semi<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:semi="https://irihi.tech/semi"
x:Class="HeadlessTest.<LibName>.App"
RequestedThemeVariant="Light">
<Application.Styles>
<semi:SemiTheme />
<!-- library theme, if any, goes after the base theme -->
</Application.Styles>
</Application>
Fix RequestedThemeVariant to Light so resource lookups and pixel colors
are deterministic. If the library ships its own theme, add it after the base
theme, e.g. <semi:UrsaSemiTheme /> (Ursa) or
<StyleInclude Source="avares://<LibName>/Themes/Generic.axaml" />, and add a
ProjectReference to the theme project if it is separate.
固定 RequestedThemeVariant="Light" 以保证资源与像素颜色可复现;库自带主题放在基础
主题之后。
App.axaml.csusing Avalonia;
using Avalonia.Markup.Xaml;
namespace HeadlessTest.<LibName>;
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
}
TestAppBuilder.cs — Rendering = Nousing Avalonia;
using Avalonia.Headless;
using HeadlessTest.<LibName>;
[assembly: AvaloniaTestApplication(typeof(TestAppBuilder))]
namespace HeadlessTest.<LibName>;
public class TestAppBuilder
{
public static AppBuilder BuildAvaloniaApp() =>
AppBuilder
.Configure<App>()
.UseHarfBuzz() // Avalonia 12 only — remove on 11.x
.UseHeadless(new AvaloniaHeadlessPlatformOptions());
}
TestAppBuilder.cs — Rendering = Yesusing Avalonia;
using Avalonia.Headless;
using HeadlessTest.<LibName>;
[assembly: AvaloniaTestApplication(typeof(TestAppBuilder))]
namespace HeadlessTest.<LibName>;
public class TestAppBuilder
{
public static AppBuilder BuildAvaloniaApp() =>
AppBuilder
.Configure<App>()
.UseSkia()
.UseHarfBuzz() // Avalonia 12 only — remove on 11.x
.UseHeadless(new AvaloniaHeadlessPlatformOptions { UseHeadlessDrawing = false });
}
Only one
[assembly: AvaloniaTestApplication]is allowed per test assembly, so every test in the project shares this builder. When the user answered Both, put the "Rendering = No" builder inHeadlessTest.<LibName>and this Skia builder inRenderTest.<LibName>. 每个测试程序集只能有一个AvaloniaTestApplication;选择 Both 时,HeadlessTest.<LibName>使用无渲染版本,RenderTest.<LibName>使用本 Skia 版本。
TestHelpers/ControlExtensions.csWorks on both Avalonia 11 and 12:
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.VisualTree;
namespace HeadlessTest.<LibName>.TestHelpers;
public static class ControlExtensions
{
public static IEnumerable<T> GetTemplateChildrenOfType<T>(this TemplatedControl control) where T : Control
{
return control.GetVisualDescendants().OfType<T>().Where(a => a.TemplatedParent == control);
}
public static T? GetTemplateChildOfType<T>(this TemplatedControl control, string name) where T : Control
{
return control.GetTemplateChildrenOfType<T>().FirstOrDefault(a => a.Name == name);
}
}
Generate tests against a real control from the library (pick one with template
parts, e.g. a PART_* constant). Use [AvaloniaFact] / [AvaloniaTheory] —
never plain [Fact], which runs off the Avalonia UI thread.
针对库中真实控件生成测试;必须使用 [AvaloniaFact] / [AvaloniaTheory],不要用 [Fact]。
using Avalonia.Controls;
using Avalonia.Headless.XUnit;
using HeadlessTest.<LibName>.TestHelpers;
using <LibNamespace>;
namespace HeadlessTest.<LibName>.Controls.<Control>Tests;
public class <Control>Tests
{
[AvaloniaFact]
public void Template_Part_Should_Exist()
{
// Arrange — controls only get their template once attached to a shown TopLevel
var window = new Window();
var control = new <Control>();
window.Content = control;
window.Show();
// Assert
var part = control.GetTemplateChildOfType<Button>(<Control>.PART_CloseButton);
Assert.NotNull(part);
}
}
using Avalonia;
using Avalonia.Controls;
using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Input;
using Avalonia.Layout;
namespace HeadlessTest.<LibName>.Controls;
public class InputTests
{
[AvaloniaFact]
public void Click_And_Type()
{
var window = new Window { Width = 200, Height = 200 };
var clicked = 0;
var button = new Button
{
Content = "OK", Width = 100, Height = 40,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
};
button.Click += (_, _) => clicked++;
window.Content = button;
window.Show();
// Mouse positions are relative to the window
window.MouseDown(new Point(10, 10), MouseButton.Left);
window.MouseUp(new Point(10, 10), MouseButton.Left);
Assert.Equal(1, clicked);
var textBox = new TextBox();
window.Content = textBox;
Avalonia.Threading.Dispatcher.UIThread.RunJobs();
textBox.Focus();
window.KeyTextInput("abc"); // text input
window.KeyPressQwerty(PhysicalKey.A, RawInputModifiers.Control); // shortcuts
Assert.Equal("abc", textBox.Text);
}
}
RenderTest.<LibName>)using System.Runtime.InteropServices;
using Avalonia.Controls;
using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using <LibNamespace>;
namespace HeadlessTest.<LibName>.Rendering;
public class <Control>RenderTests
{
[AvaloniaFact]
public void Control_Should_Render_Expected_Color()
{
var window = new Window { Width = 100, Height = 100, Background = Brushes.White };
window.Content = new <Control>
{
Width = 50, Height = 50,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
};
window.Show();
var frame = window.CaptureRenderedFrame(); // null unless Skia rendering is enabled
Assert.NotNull(frame);
// frame.Save("Control_Should_Render_Expected_Color.png"); // inspect output manually
Assert.Equal(Colors.White, GetPixel(frame, 80, 80));
}
private static Color GetPixel(WriteableBitmap bitmap, int x, int y)
{
using var buffer = bitmap.Lock();
var bytes = new byte[4];
Marshal.Copy(buffer.Address + y * buffer.RowBytes + x * 4, bytes, 0, 4);
// Pixel format depends on the platform (e.g. Rgba8888 on Linux), so check it
return buffer.Format == PixelFormat.Rgba8888
? Color.FromArgb(bytes[3], bytes[0], bytes[1], bytes[2])
: Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
}
}
dotnet sln add tests/HeadlessTest.<LibName>/HeadlessTest.<LibName>.csproj
dotnet test tests/HeadlessTest.<LibName>
# Rendering = Both
dotnet sln add tests/RenderTest.<LibName>/RenderTest.<LibName>.csproj
dotnet test tests/RenderTest.<LibName>
Like Ursa's CI, the xunit.v3 test assembly can also be executed directly, which prints failure messages and stack traces straight to the console:
与 Ursa CI 一样,xunit.v3 测试程序集可直接执行,失败信息直接输出到控制台:
dotnet build tests/HeadlessTest.<LibName> --configuration Release
dotnet tests/HeadlessTest.<LibName>/bin/Release/net10.0/HeadlessTest.<LibName>.dll
Headless tests need no display server, so they run on ubuntu-latest and
windows-latest GitHub runners without xvfb.
Run the tests after generating the files and fix any failure before reporting completion.
| Symptom / 现象 | Cause & fix / 原因与修复 |
|---|---|
Template part is null | Theme missing in App.axaml, or window.Show() not called before querying |
InvalidOperationException: The calling thread cannot access this object because a different thread owns it | Test uses [Fact] instead of [AvaloniaFact] |
CaptureRenderedFrame() returns null | Builder lacks .UseSkia() + UseHeadlessDrawing = false |
| Wrong pixel channels (red read as blue) | Ignored buffer.Format; use the GetPixel helper above |
NU1608 on xunit.v3.* | xunit.v3 older than the version required by Avalonia.Headless.XUnit |
Avalonia.HarfBuzz not found | Library is on Avalonia 11; remove the package and .UseHarfBuzz() |
| Posted work / bindings not applied yet | Call Dispatcher.UIThread.RunJobs() (and window.UpdateLayout() for layout) before asserting |
| State leaks between tests | Create a new Window per test; do not cache controls in static fields |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
avalonia, headless-testing, xunit, ui-testing, test-scaffolding
npx claudepluginhub irihitech/eureka-skills --plugin avalonia-headless-testOther plugins' skills for the same job