AgentSkillsCN

generating-tests

使用 NSubstitute 和 Shouldly 为 C#/.NET ABP 框架代码生成 xUnit 测试。适用于用户请求生成测试、创建测试覆盖率、编写单元测试、编写集成测试、填补覆盖率空白,或对某个类或文件进行测试时使用。支持 AppServices、验证器以及 HTTP API 控制器。

SKILL.md
--- frontmatter
name: generating-tests
description: "Generates xUnit tests for C#/.NET ABP Framework code using NSubstitute and Shouldly. Use when the user asks to generate tests, create test coverage, write unit tests, write integration tests, fill coverage gaps, or test a class or file. Supports AppServices, Validators, and HTTP API Controllers."

Generating Tests

Generate xUnit tests for C#/.NET ABP Framework projects.

Quick Start

python
# Analyze source and generate tests
python scripts/generate_tests.py src/MyAppService.cs --type unit

Workflow

Copy this checklist and track progress:

code
Test Generation Progress:
- [ ] Step 1: Locate source file
- [ ] Step 2: Analyze public methods and dependencies
- [ ] Step 3: Select template based on source type
- [ ] Step 4: Generate test file
- [ ] Step 5: Validate generated tests compile

Step 1: Locate source

InputAction
File pathRead directly
Class namefind api/src -name "*ClassName*.cs" -type f
gapsRun scripts/find_coverage_gaps.py

Step 2: Analyze source

Extract from each public method:

  • Method signature (name, parameters, return type)
  • Constructor dependencies → mock with Substitute.For<T>()
  • [Authorize] attributes → add 401/403 test cases
  • FluentValidation rules → add validation error tests

Step 3: Select template

Source PatternReference
*AppService.csSee reference/appservice-tests.md
*Validator.csSee reference/validator-tests.md
*Controller.csSee reference/integration-tests.md

Step 4: Generate and write

Output locations:

  • Unit tests: api/test/[Module].Application.Tests/[Class]Tests.cs
  • Integration tests: api/test/[Module].HttpApi.Tests/[Class]IntegrationTests.cs

Step 5: Validate

bash
dotnet build api/test/[Module].Application.Tests/

If build fails, fix compilation errors and rebuild.

Test Naming

Use pattern: [Method]_[Scenario]_[Expected]

ExampleMeaning
Create_ValidInput_ReturnsDtoHappy path
Create_MissingName_ThrowsValidationValidation failure
Get_NotFound_ThrowsExceptionEntity not found
Delete_Unauthorized_Returns403Permission denied