control-api-tests

Write xUnit unit tests for new public APIs on WinForms controls and components.

1.2k|337|Updated Oct 13, 2022
One-click install
npx skills add https://github.com/dotnet/dotnet --skill control-api-tests
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: control-api-tests
Source: https://github.com/dotnet/dotnet/tree/main/src/winforms/.github/skills/control-api-tests
Command: npx skills add https://github.com/dotnet/dotnet --skill control-api-tests

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Adding a new public property, method, or event to a WinForms control requires a specific, repeatable set of unit tests, and missing categories like handle-state verification or event idempotency leads to incomplete coverage and CI failures.

Core Features & Use Cases

  • Standardized Test Patterns: Provides naming conventions, the SubControl pattern for testing protected members, and data attributes like [WinFormsTheory] and [EnumData] for STA-thread-aware xUnit tests.
  • Required Test Categories: Covers default values, set/get round-trips with and without a native handle, event firing and idempotency, OnXxx virtual method invocation, and ICommand binding lifecycles.
  • CI Pitfall Guidance: Explains xUnit v3 analyzer rules such as passing CancellationToken to async calls and enabling #nullable context that fail CI builds.
  • Use Case: When you add a new DialogResult-style property to a WinForms control, use this Skill to generate the full checklist of tests including handle-state assertions and event subscription/unsubscription verification.

Quick Start

Write the complete set of unit tests for a new public property on a WinForms control following the control-api-tests conventions.

Frequently Asked Questions about control-api-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write unit tests for a new WinForms control property?

Create tests covering the default value, set/get round-trips with and without a native handle, event firing on value change, and event idempotency. Use [WinFormsFact] or [WinFormsTheory] attributes and always dispose controls with using declarations.

How to test protected OnXxx methods in WinForms controls?

Create a private nested subclass named Sub{ControlName} inside the test class that re-exposes protected members using public new. This lets tests invoke OnXxx methods directly and verify event raising behavior.

Why does my WinForms test fail CI but pass local dotnet build?

CI runs xUnit v3 analyzers as errors, including CA2016 requiring a CancellationToken in async calls and CS8632 requiring a #nullable context for nullable annotations. Verify with build.cmd instead of a single-project dotnet build.

What test attributes does the WinForms test project use?

The project uses [WinFormsFact] and [WinFormsTheory], which are STA-thread-aware xUnit attributes required for COM interop and UI operations. Data attributes like [BoolData], [EnumData<T>], and [NewAndDefaultData<EventArgs>] generate test values.

When should WinForms tests verify handle state?

Verify Assert.False(control.IsHandleCreated) in default-value and no-handle round-trip tests to ensure property access does not force handle creation. For with-handle tests, force handle creation and assert no unexpected Invalidated, StyleChanged, or HandleCreated events fire.