test-writer
When to use this skill
Use this skill whenever the user wants to:
- Write unit tests for functions, classes, or modules
- Create integration tests for API endpoints or database interactions
- Build end-to-end tests for user workflows
- Add test coverage for edge cases and error paths
- Set up a testing framework (pytest, Jest, JUnit, Vitest, Playwright)
- Generate test fixtures and mock data
How to use this skill
Workflow
- Identify the code under test - Read the function/class/module to understand its behavior
- Choose the test type - Unit (isolated), integration (multi-component), or E2E (full flow)
- Write tests using AAA pattern - Arrange inputs, Act on the function, Assert expected results
- Cover edge cases - Empty inputs, boundary values, error conditions, concurrent access
Unit Test Example (pytest)
import pytest
from decimal import Decimal
from orders import calculate_order_total, OrderItem
class TestCalculateOrderTotal:
def test_single_item_no_discount(self):
items = [OrderItem(price=Decimal("10.00"), quantity=2)]
result = calculate_order_total(items, discount_pct=0.0, tax_rate=0.08)
assert result == Decimal("21.60")
def test_applies_discount_before_tax(self):
items = [OrderItem(price=Decimal("100.00"), quantity=1)]
result = calculate_order_total(items, discount_pct=0.1, tax_rate=0.10)
assert result == Decimal("99.00")
def test_empty_items_returns_zero(self):
result = calculate_order_total([], discount_pct=0.0, tax_rate=0.08)
assert result == Decimal("0.00")
def test_invalid_discount_raises_error(self):
items = [OrderItem(price=Decimal("10.00"), quantity=1)]
with pytest.raises(ValueError, match="discount_pct must be 0-1"):
calculate_order_total(items, discount_pct=1.5)
Integration Test Example (Jest + Supertest)
import request from 'supertest';
import { app } from '../src/app';
import { db } from '../src/database';
describe('POST /api/users', () => {
afterEach(async () => { await db.query('DELETE FROM users WHERE email LIKE $1', ['%@test.com']); });
it('creates a user and returns 201', async () => {
const res = await request(app)
.post('/api/users')
.send({ name: 'Alice', email: 'alice@test.com' })
.expect(201);
expect(res.body).toMatchObject({ name: 'Alice', email: 'alice@test.com' });
});
it('returns 400 for missing email', async () => {
await request(app).post('/api/users').send({ name: 'Bob' }).expect(400);
});
});
Best Practices
- One assertion per behavior - Each test should verify one specific behavior
- Descriptive test names - Name tests as
test_<behavior>_when_<condition>orit('should <outcome> when <input>') - Isolate dependencies - Use mocks/stubs for external services, databases, and APIs
- Test the contract, not the implementation - Assert on outputs and side effects, not internal state
- Run tests in CI - Ensure tests pass on every commit; aim for 80%+ line coverage on critical paths
Keywords
测试编写, test writing, unit test, integration test, e2e test, pytest, Jest, JUnit, Vitest, Playwright, mock, fixture, 单元测试, 集成测试, 端到端测试, test coverage
More from partme-ai/full-stack-skills
vite
Guidance for Vite using the official Guide, Config Reference, and Plugins pages. Use when the user needs Vite setup, configuration, or plugin selection details.
68element-plus-vue3
Provides comprehensive guidance for Element Plus Vue 3 component library including installation, components, themes, internationalization, and API reference. Use when the user asks about Element Plus for Vue 3, needs to build Vue 3 applications with Element Plus, or customize component styles.
63vue3
Guidance for Vue 3 using the official guide and API reference. Use when the user needs Vue 3 concepts, patterns, or API details to build components, apps, and tooling.
54electron
Build cross-platform desktop applications with Electron, covering main/renderer process architecture, IPC communication, BrowserWindow management, menus, tray icons, packaging, and security best practices. Use when the user asks about Electron, needs to create desktop applications, implement Electron features, or build cross-platform desktop apps.
51uniapp-project
Provides per-component and per-API examples with cross-platform compatibility details for uni-app, covering built-in components, uni-ui components, and APIs (network, storage, device, UI, navigation, media). Use when the user needs official uni-app components or APIs, wants per-component examples with doc links, or needs platform compatibility checks.
40ascii-cli-logo-banner
Entry point for ASCII CLI banners that routes to the Python built-in font skill or figlet.js/FIGfont skill. Use when the user wants a startup banner, ASCII logo, terminal welcome screen, or CLI branding for a service.
38