# Testing Requirements (Developers)

**For:** Software Engineers developing unit/integration/E2E tests  
**Scope:** TDD workflow, code coverage (80%+), regression testing, PR verification  
**Related:** [Test Design Standards](test-design.md) for QA functional test design

## Minimum Test Coverage: 80%

Test Types (ALL required):
1. **Unit Tests** - Individual functions, utilities, components
2. **Integration Tests** - API endpoints, database operations
3. **E2E Tests** - Critical user flows (framework chosen per language)

## Test-Driven Development

MANDATORY workflow:
1. Write test first (RED)
2. Run test - it should FAIL
3. Write minimal implementation (GREEN)
4. Run test - it should PASS
5. Refactor (IMPROVE)
6. Verify coverage (80%+)

## Troubleshooting Test Failures

1. Check test isolation
2. Verify mocks are correct
3. Fix implementation, not tests (unless tests are wrong)

## PR Test Gap Analysis

Before merging, identify missing tests by categorizing each changed file:

| Change type | Tests required |
|-------------|---------------|
| New logic added | New unit tests |
| Logic modified | Updated/new unit tests + regression check |
| New API endpoint | Integration test (happy path + validation + error) |
| Bug fix | Regression test that reproduces the bug |
| Refactor only | Verify existing tests still pass — no new tests needed |
| Config/infra change | Manual verification step — document it |

### How to find gaps

1. Run `git diff main...HEAD --name-only` to get changed files
2. For each changed source file, find its test file (grep for class/function name in test dirs)
3. Check: happy path covered? edge cases (null, empty, boundary)? failure/error path?
4. List specific missing test cases with method name and scenario

### Regression test rule

Every bug fix must include a test that **fails before the fix and passes after**. No exceptions.

## Test Structure (AAA Pattern)

Prefer Arrange-Act-Assert structure for tests:

```typescript
test('calculates similarity correctly', () => {
  // Arrange
  const vector1 = [1, 0, 0]
  const vector2 = [0, 1, 0]

  // Act
  const similarity = calculateCosineSimilarity(vector1, vector2)

  // Assert
  expect(similarity).toBe(0)
})
```

### Test Naming

Use descriptive names that explain the behavior under test:

```typescript
test('returns empty array when no markets match query', () => {})
test('throws error when API key is missing', () => {})
test('falls back to substring search when Redis is unavailable', () => {})
```

---

## Risk Gate Rules (TestChecklist Approval)

**→ Full gate definitions:** `.tas/rules/qa/test-design.md` § "TestChecklist: Approval Gate Requirements"

**Policy summary (SE/DevOps context):**
- CRITICAL risks → must have mitigation or block release
- HIGH risks → must have mitigation or prioritize in /tas-functest
- Non-PE gaps (Load Testing, Penetration Testing, DR) → Risk Registry with `[NFR-NON-PE]` tag, assign to SE/DSE/DevOps
