test-data-strategy
Design and generate test data — factories, synthetic data, property-based testing, boundary analysis, contract testing, load scenarios, GDPR-safe data. Triggers: test data, synthetic data, fake data, factory, seed data, fixtures, property-based testing, fuzz testing, boundary values, contract testing, Pact, load test data, anonymize, GDPR test data.
- License 许可
- MIT
- 最近更新
- 2026-11-12
- Stars
- ★ 1
- Forks
- 1
01 前言 Front Matter
- name
- test-data-strategy
- description
- Design and generate test data — factories, synthetic data, property-based testing, boundary analysis, contract testing, load scenarios, GDPR-safe data. Triggers: test data, synthetic data, fake data, factory, seed data, fixtures, property-based testing, fuzz testing, boundary values, contract testing, Pact, load test data, anonymize, GDPR test data.
02 资源文件 2
- 参考文档evals/evals.json3.5 KB
- 参考文档references/data-generation-patterns.md10.7 KB
03 SKILL.md 正文
Test Data Strategy
Design and generate the data that powers tests. Bad test data causes false confidence (tests pass but bugs hide) and false alarms (tests fail on irrelevant data quirks). Good test data is realistic, covers edge cases, and respects privacy.
Scope Boundary
This skill designs what data to create and how to create it. For how to structure tests (naming, pyramid, framework setup), use test-suite-design. For test-first implementation, use tdd-workflow.
Workflow
Step 1: Assess Data Needs
For the feature or system under test, identify:
- What entities are involved? (Users, orders, products, transactions)
- What relationships matter? (User has many orders, order has many items)
- What data states exist? (Active user, suspended user, deleted user)
- What are the boundary values? (Empty, one item, max items, zero price, max price)
- What constitutes invalid data? (Missing required fields, wrong types, out-of-range values)
- Is production data available for reference? (Understand real distributions)
- Are there privacy constraints? (GDPR, HIPAA, PCI — cannot use real customer data)
Step 2: Choose the Right Strategy
Different testing needs require different data strategies:
| Strategy | Best For | When to Use |
|---|---|---|
| Factories | Unit/integration tests | Most tests — deterministic, focused, fast |
| Property-based testing | Algorithmic code, parsers, serialization | When exhaustive example testing is impractical |
| Boundary value analysis | Validation, calculations, range checks | Any function with numeric or string limits |
| Synthetic data generation | Load testing, staging environments | When you need volume or realistic distributions |
| Contract testing | Service-to-service boundaries | When multiple services agree on a data format |
| Snapshot testing | Complex outputs (API responses, renders) | When output is large and changes infrequently |
See references/data-generation-patterns.md for implementation of each strategy.
Step 3: Design Factory Architecture
For most applications, factories are the backbone of test data. Design them in layers:
Layer 1 — Build functions (create plain objects in memory):
buildUser({ role: 'admin' }) → { id: '...', name: '...', role: 'admin', ... }
Layer 2 — Create functions (insert into database and return):
createUser({ role: 'admin' }) → User record in DB
Layer 3 — Scenario builders (create a complete test scenario):
createCheckoutScenario() → { user, cart with 3 items, valid payment method, applied discount }
Layer 3 is the key insight most teams miss. Complex tests need complete scenarios, not individual entities assembled ad-hoc in every test.
Step 4: Define Boundary Values
For every input that has limits, test:
┌─────────────────────────────────┐
Below min │ Min │ Nominal │ Max │ Above max
└─────────────────────────────────┘
Invalid Edge Valid Edge Invalid
For each input, create test data for:
- Below minimum (invalid: -1 for quantity, empty string for name)
- At minimum (edge: 0 or 1, single character)
- Nominal (typical valid value)
- At maximum (edge: 255 characters, max integer)
- Above maximum (invalid: 256 characters, overflow value)
- Special values (null, undefined, NaN, Infinity, empty array, whitespace-only string)
Step 5: Implement Property-Based Tests
For functions where example-based tests can't cover the input space, use property-based testing:
Instead of test("sorts [3,1,2] to [1,2,3]"), assert the property: "for ANY array, sort(arr) produces a result where every element is ≤ the next."
Properties to look for:
- Roundtrip:
deserialize(serialize(x)) === x - Idempotence:
f(f(x)) === f(x)(e.g., normalization, formatting) - Invariants:
sort(arr).length === arr.length(no elements lost or added) - Commutativity:
f(a, b) === f(b, a)(order independence) - Oracle:
myFunction(x) === referenceImplementation(x)(compare against known-good)
See references/data-generation-patterns.md for framework-specific implementation.
Step 6: Handle Privacy and Compliance
When test data must resemble production data but cannot contain real PII:
Never copy production data to test environments unmasked. Instead:
- Use factories for most tests — no real data involved, no compliance risk
- Generate synthetic data for staging — realistic distributions without real identities
- Mask production data only when necessary — anonymize PII before copying to lower environments
- Version control your data generation seeds — for reproducible test runs
- Audit test environments — ensure no real PII leaked into test databases
Masking is a pipeline, not a find-and-replace. The common failure is to dump production,
run search-and-replace over the obvious columns, and call it anonymized. That leaves the data
both broken and still identifying: foreign keys and joins break when the same person is
replaced inconsistently across tables, formats stop validating (a scrambled email no longer
parses, a card number fails its checksum), free-text columns and logs keep the names that were
only scrubbed from users.name, and rare combinations — one employer in one postcode with one
birth date — re-identify a person even with every direct identifier gone.
Use a masking tool rather than a script: the dedicated anonymizers (Tonic, Gretel, Datanymizer, PostgreSQL Anonymizer) or your database's native dynamic data masking. What they supply and a hand-rolled script does not is deterministic, referentially-consistent substitution — the same input maps to the same fake value everywhere it appears, so joins survive — plus format-preserving generators and a declarative per-column policy you can review and re-run. Mask on a one-way copy, never in place.
See references/data-generation-patterns.md for privacy-safe generation patterns.
Step 7: Design Load Test Data
Load testing requires different data characteristics:
- Volume: Thousands or millions of records, not tens
- Variety: Realistic distribution of data states (not all active users)
- Uniqueness: Unique identifiers to prevent constraint violations during concurrent loads
- Pre-seeded: Data must exist before the test runs (not created during the test)
- Cleanable: Easy to identify and remove after the test (
test_prefix, specific date ranges)
Principles Applied
- KISS: Start with factories. Add property-based testing for algorithmic code. Add synthetic generation for load tests. Don't build everything on day one.
- DRY: Centralize factories in a shared test helpers directory. Every test file should import from the same factory, not define its own.
- YAGNI: Don't build a synthetic data pipeline for unit tests. Factories are sufficient for 90% of test data needs.
- Functional Independence: Test data setup should be self-contained per test. Never depend on data created by another test.
04 同仓库更多 Skills
writing-skills
Use when authoring, editing, or reviewing a skill in this library — a new SKILL.md, one that under-triggers, or hardening an existing one. Covers description and listing-budget rules, frontmatter, progressive disclosure, the 3-eval rule, and pressure-testing against rationalizations.
working-agreement
Define and validate how a multi-session project is run — where plans, status and decisions live, session routines, new work, milestone close. README/CONTRIBUTING → project-documentation.
verification-before-completion
Evidence gate before any claim that work is done, fixed, passing, or deployed. Route here when a message declares the work finished or asks to commit, push, merge, ship, or close it out, including a brief sign-off, and before git-workflow or release-management act on it. Writing the commit message or PR text itself → git-workflow.
ui-ux-design
Design user experiences from an engineer's perspective — user flows, wireframes, interaction patterns, responsive strategy, navigation, loading/error/empty states. Triggers: design the UI, user flow, wireframe, how should this screen look, loading state, error state, empty state, responsive design, navigation, page layout, form design, mockup.
threat-modeling
Design-time security analysis of a system or feature before it's built — trust boundaries, attack surface, STRIDE per element, abuse cases, prioritized mitigations that become security requirements. Triggers: threat model, STRIDE, trust boundary, attack surface, what could go wrong security-wise, security design review, abuse case, secure this design, security requirements. Reviewing EXISTING code/config for vulnerabilities → security-audit; use this skill while the design is still on the whiteboard.
test-suite-design
Design comprehensive test suites for existing code — strategy across unit/integration/e2e, fixtures, factories, helpers. Triggers: add tests, write tests for, increase coverage, test this module, testing strategy, test plan, what tests do I need, test infrastructure, test helpers, this has no tests. Not for TDD — use tdd-workflow.