Write and run tests across languages and frameworks.
| Language | Unit Tests | Integration | E2E |
|---|---|---|---|
| TypeScript/JS | Vitest (preferred), Jest | Supertest | Playwright |
| Python | pytest | pytest + httpx | Playwright |
| Swift | XCTest | XCTest | XCUITest |
npm install -D vitest @testing-library/react @testing-library/jest-dom
// vitest.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
setupFiles: './tests/setup.ts',
},
})
npx vitest # Watch mode
npx vitest run # Single run
npx vitest --coverage # With coverage
npm install -D jest @types/jest ts-jest
npx jest # Run all
npx jest --watch # Watch mode
npx jest --coverage # With coverage
npx jest path/to/test # Single file
uv pip install pytest pytest-cov pytest-asyncio httpx
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
swift test # Run all tests
swift test --filter MyTests # Specific test suite
swift test --parallel # Parallel execution
npm install -D @playwright/test
npx playwright install
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
┌─────────┐ ┌─────────┐ ┌──────────┐
│ Write │────▶│ Write │────▶│ Refactor │──┐
│ Test │ │ Code │ │ Code │ │
│ (Red) │ │ (Green) │ │ │ │
└─────────┘ └─────────┘ └──────────┘ │
▲ │
└──────────────────────────────────────────┘
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);
});
test('fetches user data', async () => {
const user = await getUser('123');
expect(user.name).toBe('Colt');
});
import { vi } from 'vitest';
const mockFetch = vi.fn().mockResolvedValue({
json: () => Promise.resolve({ id: 1, name: 'Test' }),
});
vi.stubGlobal('fetch', mockFetch);
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)
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();
});
# 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
Always test: - Public API / exported functions - Edge cases: empty input, null, boundary values - Error handling: invalid input, network failures - Business logic: calculations, state transitions
Don't bother testing: - Private implementation details - Framework internals (React rendering, Express routing) - Trivial getters/setters - Third-party library behavior
7w4.net小蔥技能站,你的AI助手技能庫。
這個 Skill 質量中等偏上,內容覆蓋面廣但不深入。它把多種測試框架的安裝、配置和常用命令整理得很清晰,還有 TDD 和測試模式的基礎指導,對新手比較友好。主要不足是內容偏基礎,像是一份速查表而非完整指南,缺少進階技巧和常見問題解答,實用性受限。