---
model: sonnet
---

# /tas-apitest $ARGUMENTS

Role: SE - Software Engineer
Generate .NET xUnit automation test project for REST API from API Test Spec file (Markdown).

## Always / Ask / Never

| | Action |
|---|---|
| **Always** | Read entire API Test Spec file before generating any test |
| **Always** | Organize tests by API version — each version separate folder |
| **Always** | Append-only: don't modify files in existing old version folder |
| **Always** | URL and credentials in `appsettings.json` — not hardcoded in code |
| **Always** | XML doc comment on each test method |
| **Ask** | When test spec unclear about expected response schema |
| **Never** | Modify or delete old version test files |
| **Never** | Hardcode base URL, API key, credentials in test code |
| **Never** | Skip error path — each endpoint needs ≥1 happy path + ≥1 error path |

## Actions

### Step 1 — Locate Input

`$ARGUMENTS` is path to API Test Spec file: `docs/tests/API-Test-Spec-{slug}.md`.

If not provided → ask user:
```
Enter path to API Test Spec file (e.g., docs/tests/API-Test-Spec-users.md)
```

### Step 2 — Parse API Test Spec

Read `.tas/rules/csharp/api-testing.md` for conventions before generating.

From API Test Spec file, collect:
- API version
- Endpoints with method, path, params
- Test cases per endpoint (TC-XXX)
- Request/response schemas
- Auth requirements
- Test data requirements
- Setup/teardown notes

### Step 3 — Detect Existing Test Project

Glob find `**/ApiTests/*.csproj`, `**/Api.Tests/*.csproj`, `**/IntegrationTests/*.csproj`.

- **Found**: Read current framework (xUnit/NUnit/MSTest), glob `v*/` folders to know existing versions.
- **Not found**: Create new at `tests/ApiTests/` with xUnit + FluentAssertions (per `csharp/testing.md`).

### Step 4 — Get API Version from Spec

Read version from API Test Spec file (find frontmatter `api_version` or section headers `## v{N}`).

Version folder: lowercase, no dot — `v1`, `v2` (not `v1.0`).

### Step 5 — Generate Infrastructure (only when creating new project)

Read `.tas/rules/csharp/api-testing.md` section **Project Structure** and **Config Pattern** to generate:
- `ApiTests.csproj` with standard packages
- `appsettings.json` (base) + `appsettings.Test.json` + `appsettings.Staging.json` with values from spec
- `Shared/TestBase.cs` — load config, HttpClient, helper methods
- `Shared/ApiTestSettings.cs` — strongly typed settings
- `.gitignore` for `appsettings.*.local.json`

### Step 6 — Generate Test Classes from Spec

Group endpoints by resource. Each group → `tests/ApiTests/{version}/{Resource}ApiTests.cs`.

For each test case (TC-XXX) in spec:
1. Read test case details
2. Map to test method with naming: `{HttpMethod}_{Resource}_Returns{Status}_When{Condition}`
3. Generate XML doc from test case title
4. Generate assertions from expected response
5. Add AC reference comment if any

Test class header comment:
```csharp
// ============================================================
// {Resource} API Tests — v{N}
// Spec: docs/tests/API-Test-Spec-{slug}.md
// Generated: {YYYY-MM-DD}
// Feature: {ID} (if applicable)
// APPEND-ONLY: don't modify existing methods.
// ============================================================
```

### Step 7 — Append-Only Guard

Before writing file: check if file already exists.
- **File doesn't exist** → create normally.
- **File exists** in version folder → check each test method:
  - Method exists (same name) → **SKIP**, don't overwrite
  - Method doesn't exist → append to end of class

### Step 8 — Generate Test Data Files (if spec requires)

If spec has `test-data.{env}.json` references:
- Create `tests/ApiTests/data/test-data.Test.json` with sample data from spec
- Guide user to create `test-data.Staging.json` and `test-data.local.json`

### Step 9 — Post-Generate Review

Launch `csharp-reviewer` agent:
> Review `tests/ApiTests/{version}/`. Read `.tas/rules/csharp/testing.md` + `.tas/rules/csharp/api-testing.md`.
> Focus: async/await, HttpClient disposal, assertion completeness, XML doc coverage.
> Format: Critical / High / Medium / Low with file:line.

Gate: Critical/High → fix immediately. Medium/Low → list suggestions.

### Step 10 — Summary

Display:
1. Test project path
2. Number of test classes generated
3. Number of test methods generated
4. Coverage matrix (vs spec)
5. Next steps to run tests

```
## API Tests Generated

**Project**: `tests/ApiTests/`
**Version**: v{N}
**Test Classes**: {N}
**Test Methods**: {N}

### Generated Files
- tests/ApiTests/{version}/{Resource}ApiTests.cs
- tests/ApiTests/Shared/TestBase.cs (if new project)

### Coverage (vs Spec)
[Print coverage table: spec test cases vs generated methods]

### Next Steps
1. Configure test environment: edit tests/ApiTests/appsettings.Test.json
2. Add test data: tests/ApiTests/data/test-data.Test.json
3. Run tests: `dotnet test tests/ApiTests/`
```

## Final Step — Token Log

Follow `.tas/rules/common/token-logging.md`: write AI Usage Log to API Test Spec file.
