介绍
# test-runner
跨语言和框架编写并运行测试。
## 框架选择
| 语言 | 单元测试 | 集成测试 | E2E | |----------|-----------|-------------|-----| | TypeScript/JS | Vitest (首选), Jest | Supertest | Playwright | | Python | pytest | pytest + httpx | Playwright | | Swift | XCTest | XCTest | XCUITest |
## 按框架快速开始
### Vitest (TypeScript / JavaScript) ```bash npm install -D vitest @testing-library/react @testing-library/jest-dom ```
```typescript // vitest.config.ts import { defineConfig } from 'vitest/config' export default defineConfig({ test: { globals: true, environment: 'jsdom', setupFiles: './tests/setup.ts', }, }) ```
```bash npx vitest # Watch mode npx vitest run # Single run npx vitest --coverage # With coverage ```
### Jest ```bash npm install -D jest @types/jest ts-jest ```
```bash npx jest # Run all npx jest --watch # Watch mode npx jest --coverage # With coverage npx jest path/to/test # Single file ```
### pytest (Python) ```bash uv pip install pytest pytest-cov pytest-asyncio httpx ```
```bash pytest # Run all pytest -v # Verbose pytest -x # Stop on first failure pytest --cov=app # With coverage pytest tests/test_api.py -k "test_login" # Specific test pytest --tb=short # Short tracebacks ```
### XCTest (Swift) ```bash swift test # Run all tests swift test --filter MyTests # Specific test suite swift test --parallel # Parallel execution ```
### Playwright (E2E) ```bash npm install -D @playwright/test npx playwright install ```
```bash npx playwright test # Run all npx playwright test --headed # With browser visible npx playwright test --debug # Debug mode npx playwright test --project=chromium # Specific browser npx playwright show-report # View HTML report ```
## TDD 工作流
1. **红(Red)** — 编写一个描述所需功能的失败测试。 2. **绿(Green)** — 编写最少的代码使测试通过。 3. **重构(Refactor)** — 在保持测试通过的同时清理代码。
``` ┌─────────┐ ┌─────────┐ ┌──────────┐ │ Write │────▶│ Write │────▶│ Refactor │──┐ │ Test │ │ Code │ │ Code │ │ │ (Red) │ │ (Green) │ │ │ │ └─────────┘ └─────────┘ └──────────┘ │ ▲ │ └──────────────────────────────────────────┘ ```
## 测试模式
### 准备-执行-断言(Arrange-Act-Assert) ```typescript test('calculates total with tax', () => { // Arrange const cart = new Cart([{ price: 100, qty: 2 }]);
// Act const total = cart.totalWithTax(0.08);
// Assert expect(total).toBe(216); }); ```
### 测试异步代码 ```typescript test('fetches user data', async () => { const user = await getUser('123'); expect(user.name).toBe('Colt'); }); ```
### 模拟 ```typescript import { vi } from 'vitest';
const mockFetch = vi.fn().mockResolvedValue({ json: () => Promise.resolve({ id: 1, name: 'Test' }), }); vi.stubGlobal('fetch', mockFetch); ```
### 测试 API 端点 (Python) ```python import pytest from httpx import AsyncClient from app.main import app
@pytest.mark.asyncio async def test_get_users(): async with AsyncClient(app=app, base_url="http://test") as client: response = await client.get("/users") assert response.status_code == 200 assert isinstance(response.json(), list) ```
### 测试 React 组件 ```typescript import { render, screen, fireEvent } from '@testing-library/react'; import { Button } from './Button';
test('calls onClick when clicked', () => { const handleClick = vi.fn(); render(<Button onClick={handleClick}>Click me</Button>); fireEvent.click(screen.getByText('Click me')); expect(handleClick).toHaveBeenCalledOnce(); }); ```
## 覆盖率命令
```bash # JavaScript/TypeScript npx vitest --coverage # Vitest (uses v8 or istanbul) npx jest --coverage # Jest
# Python pytest --cov=app --cov-report=html # HTML report pytest --cov=app --cov-report=term # Terminal output pytest --cov=app --cov-fail-under=80 # Fail if < 80%
# View HTML coverage report open coverage/index.html # macOS open htmlcov/index.html # Python ```
## 测试什么
**始终测试:** - 公共 API / 导出的函数 - 边缘情况:空输入、null、边界值 - 错误处理:无效输入、网络故障 - 业务逻辑:计算、状态转换
**无需测试:** - 私有实现细节 - 框架内部(React 渲染、Express 路由) - 简单的 getter/setter - 第三方库的行为