Skill站外收录 @skilllib/skill-2117939930272417093
write-tests
生成可复现 GitHub 问题的 UI 测试,确保测试在未修复前执行失败,以验证其有效性。涵盖跨平台场景,遵循 .NET MAUI 测试规范,实现从界面构建到行为断言的完整覆盖,用于补充缺失测试或构建缺陷复现用例。
测试与安全 AI 技能
Skills.MD
# Write Tests Skill
Creates UI tests that reproduce a GitHub issue, following .NET MAUI conventions. **Verifies the tests actually fail before completing.**
## When to Use
- ✅ PR has no tests and needs them
- ✅ Issue needs a reproduction test before fixing
- ✅ Existing tests don't adequately cover the bug
## Required Input
Before invoking, ensure you have:
- **Issue number** (e.g., 33331)
- **Issue description** or reproduction steps
- **Platforms affected** (iOS, Android, Windows, MacCatalyst)
## Workflow
### Step 1: Read the UI Test Guidelines
```bash
cat .github/instructions/uitests.instructions.md
```
This contains the authoritative conventions for:
- File naming (`IssueXXXXX.xaml`, `IssueXXXXX.cs`)
- File locations (`TestCases.HostApp/Issues/`, `TestCases.Shared.Tests/Tests/Issues/`)
- Required attributes (`[Issue()]`, `[Category()]`)
- Test patterns and assertions
### Step 2: Create HostApp Page
**Location:** `src/Controls/tests/TestCases.HostApp/Issues/IssueXXXXX.cs`
```csharp
namespace Maui.Controls.Sample.Issues;
[Issue(IssueTracker.Github, XXXXX, "Brief description of issue", PlatformAffected.All)]
public partial class IssueXXXXX : ContentPage
{
public IssueXXXXX()
{
// Create UI that reproduces the issue
var button = new Button
{
Text = "Test Button",
AutomationId = "TestButton" // Required for Appium
};
var resultLabel = new Label
{
Text = "Waiting...",
AutomationId = "ResultLabel"
};
button.Clicked += (s, e) =>
{
resultLabel.Text = "Success";
};
Content = new VerticalStackLayout
{
Children = { button, resultLabel }
};
}
}
```
**Key requirements:**
- Add `AutomationId` to all interactive elements
- Use `[Issue()]` attribute with tracker, number, description, platform
- Keep UI minimal - just enough to reproduce the bug
### Step 3: Create NUnit Test
**Location:** `src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/IssueXXXXX.cs`
```csharp
namespace Microsoft.Maui.TestCases.Shared.Tests.Tests.Issues;
public class IssueXXXXX : _IssuesUITest
{
public override string Issue => "Brief description matching HostApp";
public IssueXXXXX(TestDevice device) : base(device) { }
[Test]
[Category(UITestCategories.Button)] // Pick ONE appropriate category
public void ButtonClickUpdatesLabel()
{
// Wait for element to be ready
App.WaitForElement("TestButton");
// Interact with the UI
App.Tap("TestButton");
// Verify expected behavior
var labelText = App.FindElement("ResultLabel").GetText();
Assert.That(labelText, Is.EqualTo("Success"));
}
}
```
**Key requirements:**
- Inherit from `_IssuesUITest`
- Use same `AutomationId` values as HostApp
- Add ONE `[Category()]` attribute (check `UITestCategories.cs` for options)
- Use `App.WaitForElement()` before interactions
### Step 4: Verify Files Compile
```bash
dotnet build src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj -c Debug -f net10.0-android --no-restore -v q
dotnet build src/Controls/tests/TestCases.Shared.Tests/Controls.TestCases.Shared.Tests.csproj -c Debug --no-restore -v q
```
### Step 5: Verify Tests Reproduce the Bug ⚠️ CRITICAL
**Tests must FAIL to prove they catch the bug.** Run verification:
```bash
pwsh .github/skills/verify-tests-fail-without-fix/scripts/verify-tests-fail.ps1 -Platform ios -TestFilter "IssueXXXXX"
```
The script auto-detects that only test files exist (no fix files) and runs in "verify failure only" mode.
**If tests FAIL** → ✅ Success! Tests correctly reproduce the bug.
**If tests PASS** → ❌ Your test is wrong. Go back to Step 2 and fix:
- Review test scenario against issue description
- Ensure test actions match reproduction steps
- Update and rerun until tests FAIL
**Do NOT mark this skill complete until tests FAIL.**
## Output
After completion (tests verified to fail), report:
```markdown
✅ Tests created and verified for Issue #XXXXX
**Files:**
- `src/Controls/tests/TestCases.HostApp/Issues/IssueXXXXX.cs`
- `src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/IssueXXXXX.cs`
**Test method:** `ButtonClickUpdatesLabel`
**Category:** `UITestCategories.Button`
**Verification:** Tests FAIL as expected (bug reproduced)
```
## Common Patterns
### Testing Property Changes
```csharp
// HostApp: Add a way to trigger and observe the property
var picker = new Picker { AutomationId = "TestPicker" };
var statusLabel = new Label { AutomationId = "StatusLabel" };
picker.PropertyChanged += (s, e) => {
if (e.PropertyName == nameof(Picker.IsOpen))
statusLabel.Text = $"IsOpen={picker.IsOpen}";
};
// Test: Verify the property changes correctly
App.Tap("TestPicker");
App.WaitForElement("StatusLabel");
var status = App.FindElement("StatusLabel").GetText();
Assert.That(status, Does.Contain("IsOpen=True"));
```
### Testing Layout/Positioning
```csharp
// Test: Use GetRect() for position/size assertions
var rect = App.WaitForElement("TestElement").GetRect();
Assert.That(rect.Height, Is.GreaterThan(0));
Assert.That(rect.Y, Is.GreaterThanOrEqualTo(safeAreaTop));
```
### Testing Platform-Specific Behavior
```csharp
// Only limit platforms when NECESSARY
[Test]
[Category(UITestCategories.Picker)]
public void PickerDismissResetsIsOpen()
{
// This test should run on all platforms unless there's
// a specific technical reason it can't
App.WaitForElement("TestPicker");
// ...
}
```
## References
- **Full conventions:** `.github/instructions/uitests.instructions.md`
- **Category list:** `src/Controls/tests/TestCases.Shared.Tests/UITestCategories.cs`
- **Example tests:** `src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/`
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「write-tests」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次