使用推荐框架编写、运行和管理 TypeScript、Python 和 Swift 的单元、集成及 E2E 测试。
测试运行器 : 写入并运行跨语言和框架的测试. : 框架选择. 语言是一项面向实际任务的技能,主要用于单元测试. 集成.;E2E. 端口脚本/JS.;
Python.pytest.pytest +。它将相关步骤、工具调用和结果整理方式集中到统一流程中,帮助使用者更快完成目标并减少重复操作。使用时应结合输入条件选择合适的执行方式,核对必要参数、依赖环境与输出内容,并按原始要求处理异常情况。从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
跨语言与框架编写并运行测试。
| 语言 | 单元测试 | 集成测试 | 端到端(E2E)测试 |
|---|---|---|---|
| TypeScript/JS | Vitest(推荐)、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 # 启用覆盖率统计
npm install -D jest @types/jest ts-jest
npx jest # 运行全部测试
npx jest --watch # 监听模式(Watch mode)
npx jest --coverage # 启用覆盖率统计
npx jest path/to/test # 运行指定文件
uv pip install pytest pytest-cov pytest-asyncio httpx
pytest # 运行全部测试
pytest -v # 详细输出(Verbose)
pytest -x # 首次失败即停止(Stop on first failure)
pytest --cov=app # 启用覆盖率统计(覆盖 app 模块)
pytest tests/test_api.py -k "test_login" # 运行匹配名称的特定测试
pytest --tb=short # 简化堆栈跟踪(Short tracebacks)
swift test # 运行全部测试
swift test --filter MyTests # 运行指定测试套件
swift test --parallel # 并行执行
npm install -D @playwright/test
npx playwright install
npx playwright test # 运行全部测试
npx playwright test --headed # 显示浏览器界面(With browser visible)
npx playwright test --debug # 调试模式(Debug mode)
npx playwright test --project=chromium # 指定浏览器(如 Chromium)
npx playwright show-report # 查看 HTML 测试报告
┌─────────┐ ┌─────────┐ ┌──────────┐
│ 编写 │────▶│ 编写 │────▶│ 重构 │──┐
│ 测试 │ │ 代码 │ │ 代码 │ │
│ (红) │ │ (绿) │ │ │ │
└─────────┘ └─────────┘ └──────────┘ │
▲ │
└──────────────────────────────────────────┘
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();
fireEvent.click(screen.getByText('Click me'));
expect(handleClick).toHaveBeenCalledOnce();
});
# JavaScript/TypeScript
npx vitest --coverage # Vitest(默认使用 v8 或 istanbul)
npx jest --coverage # Jest
# Python
pytest --cov=app --cov-report=html # 生成 HTML 报告
pytest --cov=app --cov-report=term # 终端输出覆盖率
pytest --cov=app --cov-fail-under=80 # 覆盖率低于 80% 则构建失败
# 查看 HTML 覆盖率报告
open coverage/index.html # macOS(Vitest/Jest)
open htmlcov/index.html # Python(pytest-cov)
务必覆盖:
无需测试: