Scaffold Test Files
ASecurityAutomatically create matching test directory structure and test files for a given .NET source file. USE FOR: when a user selects a source file and wants unit and/or integration test files created, mirroring the source project's folder structure under the test project. Detects existing test project layout (separate or unified), creates directories and boilerplate test files following clean DI patterns read from the actual project. DO NOT USE FOR: writing test logic (use improve-test-coverage),...
- 3 stars
- 0 votes
- 0 copies
- 1 view
- Added November 20, 2026
Works with
Security analysis
100/100Pro scans all 3 files and shows the line behind each finding
npx -y skills add Hyeonu-Cha/dotnet-coverage-mcp --skill scaffold-test-files --agent claude-codeAre you the author of Scaffold Test Files?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hyeonu-cha-scaffold-test-files)---
name: scaffold-test-files
description: "Automatically create matching test directory structure and test files for a given .NET source file. USE FOR: when a user selects a source file and wants unit and/or integration test files created, mirroring the source project's folder structure under the test project. Detects existing test project layout (separate or unified), creates directories and boilerplate test files following clean DI patterns read from the actual project. DO NOT USE FOR: writing test logic (use improve-test-coverage), running tests (use run-coverage)."
---
# Scaffold Test Files
Given a .NET source file, automatically create matching unit test and integration test directories and files under the test project, following the project's existing conventions and clean DI patterns.
## When to Use
- User selects a source file and wants test files created for it
- User asks to "set up tests for this file" or "create test structure for X"
- Starting a new coverage session where no test file exists yet
- Adding integration test support alongside existing unit tests
## When Not to Use
- Test files already exist for the source file — use `improve-test-coverage` instead
- User only wants to run existing tests — use `run-coverage`
- User wants to inspect coverage gaps — use `analyze-coverage-gaps`
- Non-.NET project
## Inputs
| Input | Required | Description |
|-------|----------|-------------|
| `sourceFilePath` | Yes | Full path to the `.cs` source file to create tests for |
| `testTypes` | No | Comma-separated list: `unit`, `integration`, or both (default: both) |
## Workflow
### Step 1: Analyze source file
Call `describe_test_target` on the source file. Per class it returns:
- **Namespace** and **class name**
- **Dependencies** — each constructor parameter with its `kind` and `howToProvide` (mock an interface, `NullLogger<T>.Instance` for a logger, `Options.Create(...)` for options, a real instance for a sealed or concrete class, SQLite in-memory for a `DbContext`, ...)
- **Methods** — public and internal signatures to create test stubs for, with `decisionPoints` as a rough count of tests needed
- **suggestedTestType** (`unit` / `integration` / `either`) with `reasons`, which decide between Step 6 and Step 7
- **testFiles** — per class, the test file to use: `unit` and `integration`, each with `fileName` and `existing` (see Step 3)
- **existingTestFiles** — every test file found for the source file; if the class already has one, extend it instead (use `improve-test-coverage`)
### Step 2: Detect test project structure
Scan the solution for test projects:
```
glob **/*Test*.csproj
glob **/*Tests*.csproj
```
Determine the layout:
- **Separate projects**: `UnitTests.csproj` and `IntegrationTests.csproj` exist separately → use each
- **Single project**: One test `.csproj` with folders like `Unit/` and `Integration/` inside → use subfolders
- **No test project**: Inform the user they need to create one first
### Step 3: Mirror directory structure
Map the source path to test paths by mirroring the subfolder structure:
**Source:** `MainProject/Subproject/Services/ExampleService.cs`
**Separate projects:**
- `TestProject.UnitTests/Subproject/Services/ExampleServiceUnitTests.cs`
- `TestProject.IntegrationTests/Subproject/Services/ExampleServiceIntegrationTests.cs`
**Single project:**
- `TestProject/UnitTests/Subproject/Services/ExampleServiceUnitTests.cs`
- `TestProject/IntegrationTests/Subproject/Services/ExampleServiceIntegrationTests.cs`
Create any missing directories.
**Test file naming: one test file per class, named by `describe_test_target`**
- Write each class's tests to `testFiles.<unit|integration>.existing` when it is set. Never create a second file for the same class and test type.
- Otherwise create `testFiles.<unit|integration>.fileName` in the mirrored folder above. Use no other file name: the server has already matched the suffix most of the repository's test files use (`UnitTests`, `Tests` or `Test`; `UnitTests` when there are none or on a tie), dropped generic type parameters, and given each class in a multi-class source file its own file.
- Never create grouped files (`*GapTests.cs`, `ServicesTests.cs`, `ControllersTests.cs`). Leave existing grouped files where they are.
- Shared test infrastructure (`WebApplicationFactory` subclasses, `TestAuthHandler`, builders) goes in its own helper files, not in test classes.
### Step 4: Read project conventions
Before generating boilerplate, read 2–3 existing test files in the test project to learn:
- **Using statements** — which namespaces are imported
- **Mock framework** — Moq (`Mock<T>`), NSubstitute (`Substitute.For<T>`), or FakeItEasy
- **Base class** — whether tests inherit from a base test class
- **Test framework** — call `get_test_environment` on the test project: it returns the framework, the attributes to use (`conventions`), and the mocking and assertion libraries
- **Naming convention** — `MethodName_Condition_Result` or other patterns
- **DI setup patterns** — how `ServiceCollection` or `WebApplicationFactory` is configured
If there are no existing test files, follow `get_test_environment`'s `conventions`, with clean DI patterns. Never mix attributes from different frameworks.
### Step 5: Read context-specific rules
Based on test type being generated:
- **Unit tests** → read `references/unit.md` for rules
- **Integration tests** → read `references/integration.md` for rules
Apply these rules when generating the boilerplate.
### Step 6: Generate unit test file
Create the unit test file with boilerplate matching the project's conventions:
- Namespace mirroring source namespace
- Test class in the project's framework style (NUnit `[TestFixture]`, MSTest `[TestClass]`, xUnit plain class)
- Per-test setup (xUnit constructor, NUnit `[SetUp]`, MSTest `[TestInitialize]`) creating mocks for all injected interfaces
- Instantiating the class under test with mocked dependencies
- Empty test method stubs for each public method (no assertions — those come from `improve-test-coverage`)
### Step 7: Generate integration test file
Create the integration test file with clean DI + in-memory infrastructure:
- Namespace mirroring source namespace
- Test class in the project's framework style
- Per-test setup building a real `ServiceCollection` with:
- SQLite in-memory database (`UseSqlite` on an open `DataSource=:memory:` connection, then `EnsureCreated()`) if EF Core `DbContext` is used — not `UseInMemoryDatabase`. If the model doesn't build on SQLite, follow "When SQLite can't create the model" in `references/integration.md`
- Real service registrations from the project's DI composition root
- Only external services mocked (HTTP clients, third-party APIs)
- `ServiceProvider` built and class under test resolved from a scope, by the type it is registered as (usually its interface)
- Empty test method stubs for each public method
### Step 8: Restore NuGet packages
**CRITICAL:** If you created a new test project or added NuGet packages (Moq, NUnit, Microsoft.NET.Test.Sdk, etc.), you MUST run `dotnet restore` before any test execution. When calling `run_tests_with_coverage` after scaffolding, set `forceRestore=true` on the first run.
### Step 9: Update target .gitignore
If the target repository's `.gitignore` does not already contain `.mcp-coverage/`, add it. This directory holds session-scoped coverage state files generated by the MCP server.
### Step 10: Report created files
List all created directories and files with their paths.
## Validation
- [ ] Source file was read and class/constructor/methods extracted
- [ ] Test project structure detected correctly (separate vs unified)
- [ ] Directory structure mirrors the source path
- [ ] Each test file is named exactly as `testFiles` gives it, or is the `existing` file
- [ ] Unit test file follows project's existing mock framework and conventions
- [ ] Integration test file uses in-memory infrastructure with real DI
- [ ] No hardcoded templates — boilerplate matches what's already in the project
- [ ] Created files compile (no missing usings or references)
## Common Pitfalls
| Pitfall | Solution |
|---------|----------|
| No test project found | Inform user to create a test `.csproj` first |
| Mixed conventions in existing tests | Follow the most common pattern (majority wins) |
| Source class has no constructor injection | Still create the test file; note it may use static methods or `new` directly |
| EF Core not used but integration test requested | Use `ServiceCollection` without in-memory DB; register real services with mocked external dependencies |
| Namespace mismatch | Always derive test namespace from the test project root namespace + mirrored subfolder path |
Files in this skill
- SKILL.md
- references/integration.md
- references/unit.md
Attribution
Related skills
View all in ai-agents →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.