# Tests Horizontal Instructions
## Purpose
The Tests horizontal defines comprehensive testing strategies, test cases, and quality assurance procedures for each vertical component. It ensures system reliability, performance, and maintainability through systematic validation across all layers.
## Content Requirements
### Required Sections
1. **Test Strategy**
- Testing pyramid implementation (Unit, Integration, E2E)
- Test coverage requirements and thresholds
- Testing frameworks and tools selection
- Continuous integration/deployment testing
2. **Unit Tests**
- Individual function and method testing
- Mock and stub strategies
- Test data generation and management
- Boundary condition testing
3. **Integration Tests**
- Component interaction testing
- Database integration testing
- API contract testing
- Third-party service integration
4. **End-to-End Tests**
- User journey testing
- Cross-browser/platform testing
- Performance and load testing
- Security and penetration testing
5. **Quality Metrics**
- Code coverage analysis
- Performance benchmarks
- Security vulnerability scanning
- Accessibility compliance testing
### Documentation Format
```yaml
test_suites:
unit:
framework: "jest"
coverage_threshold: 90
patterns:
- "src/**/*.test.js"
- "src/**/*.spec.js"
setup:
- "tests/setup/unit.js"
integration:
framework: "supertest"
database: "test_database"
patterns:
- "tests/integration/**/*.test.js"
setup:
- "tests/setup/database.js"
- "tests/setup/server.js"
e2e:
framework: "playwright"
browsers: ["chromium", "firefox", "webkit"]
patterns:
- "tests/e2e/**/*.spec.js"
setup:
- "tests/setup/e2e-environment.js"
```
## Validation Rules
### Quality Control
1. **Coverage Requirements**
- Minimum 80% line coverage for unit tests
- Minimum 70% branch coverage
- 100% coverage for critical business logic
- Integration test coverage for all API endpoints
2. **Test Quality**
- Tests must be deterministic and repeatable
- Tests must be independent and isolated
- Test names must clearly describe the scenario
- Assertions must be specific and meaningful
3. **Performance Standards**
- Unit tests must complete within 100ms each
- Integration tests must complete within 5 seconds each
- E2E tests must complete within 30 seconds each
- Test suite must complete within defined CI/CD timeouts
4. **Maintainability**
- Tests must be refactored alongside production code
- Test data must be managed and version controlled
- Flaky tests must be identified and fixed
- Test documentation must be kept current
### Automated Checks
- Coverage threshold enforcement
- Test execution time monitoring
- Flaky test detection
- Security vulnerability scanning
- Performance regression detection
## Evolution Rules
### Across Verticals (Left to Right)
1. **Foundation → Core**
- Basic unit tests evolve to include business logic validation
- Simple mocks become sophisticated test doubles
- Manual testing becomes automated testing
2. **Core → Integration**
- Internal testing expands to external API testing
- Single-service tests become multi-service orchestration
- Synchronous testing includes asynchronous patterns
3. **Integration → Extension**
- Fixed test scenarios become configurable test suites
- Standard testing includes plugin and extension testing
- Monolithic tests become distributed testing strategies
### Test Maturity Levels
- **Level 1**: Basic unit tests with minimal coverage
- **Level 2**: Comprehensive testing with integration coverage
- **Level 3**: Advanced testing with performance and security
- **Level 4**: AI-assisted testing with predictive quality metrics
## Dependencies
### Upstream Dependencies
1. **Specs Horizontal**
- Functional requirements for test scenarios
- Acceptance criteria for test validation
- Performance requirements for benchmark tests
2. **Schema Horizontal**
- Data models for test fixture generation
- Validation rules for input/output testing
- Relationship constraints for integration testing
3. **API Horizontal**
- Interface contracts for API testing
- Request/response schemas for validation
- Authentication patterns for security testing
### Downstream Dependencies
1. **Frontend Horizontal**
- UI component testing strategies
- User interaction testing patterns
- Cross-browser compatibility requirements
2. **Backend Horizontal**
- Service implementation testing
- Database interaction testing
- Performance optimization validation
## Best Practices
### Testing Principles
1. **Test-Driven Development (TDD)**
- Write tests before implementation
- Red-Green-Refactor cycle
- Focus on behavior specification
- Maintain test-first discipline
2. **Behavior-Driven Development (BDD)**
- Use natural language specifications
- Focus on user scenarios and outcomes
- Collaborate with stakeholders on test scenarios
- Maintain living documentation
3. **Testing Pyramid**
- 70% Unit Tests (fast, isolated, focused)
- 20% Integration Tests (moderate speed, realistic)
- 10% End-to-End Tests (slow, comprehensive, realistic)
4. **Quality Gates**
- All tests must pass before deployment
- Coverage thresholds must be met
- Performance benchmarks must be maintained
- Security scans must show no critical issues
### Common Testing Patterns
```javascript
// Unit Test Pattern
describe('UserService', () => {
let userService;
let mockRepository;
beforeEach(() => {
mockRepository = createMock(UserRepository);
userService = new UserService(mockRepository);
});
describe('createUser', () => {
it('should create user with valid data', async () => {
// Arrange
const userData = { email: 'test@example.com', name: 'Test User' };
const expectedUser = { id: '123', ...userData };
mockRepository.create.mockResolvedValue(expectedUser);
// Act
const result = await userService.createUser(userData);
// Assert
expect(result).toEqual(expectedUser);
expect(mockRepository.create).toHaveBeenCalledWith(userData);
});
it('should throw error for invalid email', async () => {
// Arrange
const invalidData = { email: 'invalid-email', name: 'Test' };
// Act & Assert
await expect(userService.createUser(invalidData))
.rejects
.toThrow('Invalid email format');
});
});
});
// Integration Test Pattern
describe('User API Integration', () => {
let app;
let database;
beforeAll(async () => {
database = await setupTestDatabase();
app = createTestApp(database);
});
afterAll(async () => {
await cleanupTestDatabase(database);
});
beforeEach(async () => {
await database.clear();
});
it('should create and retrieve user', async () => {
// Create user
const createResponse = await request(app)
.post('/api/users')
.send({ email: 'test@example.com', name: 'Test User' })
.expect(201);
const userId = createResponse.body.id;
// Retrieve user
const getResponse = await request(app)
.get(`/api/users/${userId}`)
.expect(200);
expect(getResponse.body).toMatchObject({
id: userId,
email: 'test@example.com',
name: 'Test User'
});
});
});
// E2E Test Pattern
describe('User Registration Flow', () => {
let page;
beforeAll(async () => {
page = await browser.newPage();
});
afterAll(async () => {
await page.close();
});
it('should allow user to register and login', async () => {
// Navigate to registration page
await page.goto('/register');
// Fill registration form
await page.fill('[data-testid="email-input"]', 'test@example.com');
await page.fill('[data-testid="password-input"]', 'SecurePassword123');
await page.fill('[data-testid="name-input"]', 'Test User');
// Submit form
await page.click('[data-testid="register-button"]');
// Verify success message
await expect(page.locator('[data-testid="success-message"]'))
.toContainText('Registration successful');
// Navigate to login
await page.click('[data-testid="login-link"]');
// Login with created account
await page.fill('[data-testid="email-input"]', 'test@example.com');
await page.fill('[data-testid="password-input"]', 'SecurePassword123');
await page.click('[data-testid="login-button"]');
// Verify successful login
await expect(page).toHaveURL('/dashboard');
await expect(page.locator('[data-testid="welcome-message"]'))
.toContainText('Welcome, Test User');
});
});
```
## Test Data Management
### Fixture Patterns
```javascript
// Factory Pattern
class UserFactory {
static create(overrides = {}) {
return {
id: faker.string.uuid(),
email: faker.internet.email(),
name: faker.person.fullName(),
createdAt: faker.date.past(),
...overrides
};
}
static createMany(count, overrides = {}) {
return Array.from({ length: count }, () => this.create(overrides));
}
}
// Builder Pattern
class UserBuilder {
constructor() {
this.user = {
id: faker.string.uuid(),
email: faker.internet.email(),
name: faker.person.fullName(),
createdAt: new Date()
};
}
withEmail(email) {
this.user.email = email;
return this;
}
withName(name) {
this.user.name = name;
return this;
}
asAdmin() {
this.user.role = 'admin';
return this;
}
build() {
return { ...this.user };
}
}
```
## Performance Testing
### Load Testing Strategy
```yaml
performance_tests:
load_tests:
tool: "artillery"
scenarios:
- name: "user_registration_flow"
arrival_rate: 10
duration: 60
requests:
- post: "/api/users"
json:
email: "{{ $randomEmail() }}"
name: "{{ $randomName() }}"
stress_tests:
tool: "k6"
scenarios:
- name: "high_concurrency"
executor: "ramping-vus"
start_vus: 0
stages:
- duration: "2m", target: 100
- duration: "5m", target: 100
- duration: "2m", target: 200
- duration: "5m", target: 200
- duration: "2m", target: 0
benchmark_tests:
response_time:
p95: "< 500ms"
p99: "< 1000ms"
throughput:
min_rps: 100
error_rate:
max_percentage: 1
```
## Security Testing
### Security Test Categories
1. **Authentication Testing**
- Login/logout functionality
- Password policies
- Session management
- Multi-factor authentication
2. **Authorization Testing**
- Role-based access control
- Permission boundaries
- Privilege escalation
- Resource access control
3. **Input Validation Testing**
- SQL injection prevention
- XSS attack prevention
- CSRF protection
- Input sanitization
4. **API Security Testing**
- Rate limiting
- API key validation
- Request signing
- Data encryption
## Integration Guidelines
### CI/CD Integration
```yaml
# GitHub Actions Example
name: Test Pipeline
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Run unit tests
run: npm run test:unit
- name: Run integration tests
run: npm run test:integration
- name: Run E2E tests
run: npm run test:e2e
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage/lcov.info
- name: Security scan
run: npm audit --audit-level moderate
```
### Tools and Frameworks
- **Unit Testing**: Jest, Mocha, Vitest, Jasmine
- **Integration Testing**: Supertest, TestContainers, Docker Compose
- **E2E Testing**: Playwright, Cypress, Selenium, Puppeteer
- **Performance Testing**: Artillery, K6, JMeter, Gatling
- **Security Testing**: OWASP ZAP, Snyk, SonarQube, Burp Suite
- **Coverage Analysis**: Istanbul, NYC, C8
- **Test Data**: Faker.js, Chance.js, Factory Boy