test-data-management

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Test Data Management

测试数据管理

Create and manage test data effectively.
高效创建和管理测试数据。

Factory Pattern (Python)

工厂模式(Python)

python
from factory import Factory, Faker, SubFactory, LazyAttribute
from app.models import User, Analysis

class UserFactory(Factory):
    class Meta:
        model = User

    email = Faker('email')
    name = Faker('name')
    created_at = Faker('date_time_this_year')

class AnalysisFactory(Factory):
    class Meta:
        model = Analysis

    url = Faker('url')
    status = 'pending'
    user = SubFactory(UserFactory)

    @LazyAttribute
    def title(self):
        return f"Analysis of {self.url}"
python
from factory import Factory, Faker, SubFactory, LazyAttribute
from app.models import User, Analysis

class UserFactory(Factory):
    class Meta:
        model = User

    email = Faker('email')
    name = Faker('name')
    created_at = Faker('date_time_this_year')

class AnalysisFactory(Factory):
    class Meta:
        model = Analysis

    url = Faker('url')
    status = 'pending'
    user = SubFactory(UserFactory)

    @LazyAttribute
    def title(self):
        return f"Analysis of {self.url}"

Usage

Usage

user = UserFactory() analysis = AnalysisFactory(user=user, status='completed')
undefined
user = UserFactory() analysis = AnalysisFactory(user=user, status='completed')
undefined

Factory Pattern (TypeScript)

工厂模式(TypeScript)

typescript
import { faker } from '@faker-js/faker';

interface User {
  id: string;
  email: string;
  name: string;
}

const createUser = (overrides: Partial<User> = {}): User => ({
  id: faker.string.uuid(),
  email: faker.internet.email(),
  name: faker.person.fullName(),
  ...overrides,
});

const createAnalysis = (overrides = {}) => ({
  id: faker.string.uuid(),
  url: faker.internet.url(),
  status: 'pending',
  userId: createUser().id,
  ...overrides,
});

// Usage
const user = createUser({ name: 'Test User' });
const analysis = createAnalysis({ userId: user.id, status: 'completed' });
typescript
import { faker } from '@faker-js/faker';

interface User {
  id: string;
  email: string;
  name: string;
}

const createUser = (overrides: Partial<User> = {}): User => ({
  id: faker.string.uuid(),
  email: faker.internet.email(),
  name: faker.person.fullName(),
  ...overrides,
});

const createAnalysis = (overrides = {}) => ({
  id: faker.string.uuid(),
  url: faker.internet.url(),
  status: 'pending',
  userId: createUser().id,
  ...overrides,
});

// Usage
const user = createUser({ name: 'Test User' });
const analysis = createAnalysis({ userId: user.id, status: 'completed' });

JSON Fixtures

JSON Fixture

json
// fixtures/users.json
{
  "admin": {
    "id": "user-001",
    "email": "admin@example.com",
    "role": "admin"
  },
  "basic": {
    "id": "user-002",
    "email": "user@example.com",
    "role": "user"
  }
}
python
import json
import pytest

@pytest.fixture
def users():
    with open('fixtures/users.json') as f:
        return json.load(f)

def test_admin_access(users):
    admin = users['admin']
    assert admin['role'] == 'admin'
json
// fixtures/users.json
{
  "admin": {
    "id": "user-001",
    "email": "admin@example.com",
    "role": "admin"
  },
  "basic": {
    "id": "user-002",
    "email": "user@example.com",
    "role": "user"
  }
}
python
import json
import pytest

@pytest.fixture
def users():
    with open('fixtures/users.json') as f:
        return json.load(f)

def test_admin_access(users):
    admin = users['admin']
    assert admin['role'] == 'admin'

Database Seeding

数据库数据填充

python
undefined
python
undefined

seeds/test_data.py

seeds/test_data.py

async def seed_test_database(db: AsyncSession): """Seed database with test data.""" # Create users users = [ UserFactory.build(email=f"user{i}@test.com") for i in range(10) ] db.add_all(users)
# Create analyses for each user
for user in users:
    analyses = [
        AnalysisFactory.build(user_id=user.id)
        for _ in range(5)
    ]
    db.add_all(analyses)

await db.commit()
@pytest.fixture async def seeded_db(db_session): await seed_test_database(db_session) yield db_session
undefined
async def seed_test_database(db: AsyncSession): """Seed database with test data.""" # Create users users = [ UserFactory.build(email=f"user{i}@test.com") for i in range(10) ] db.add_all(users)
# Create analyses for each user
for user in users:
    analyses = [
        AnalysisFactory.build(user_id=user.id)
        for _ in range(5)
    ]
    db.add_all(analyses)

await db.commit()
@pytest.fixture async def seeded_db(db_session): await seed_test_database(db_session) yield db_session
undefined

Fixture Composition

Fixture组合

python
@pytest.fixture
def user():
    return UserFactory()

@pytest.fixture
def user_with_analyses(user):
    analyses = [AnalysisFactory(user=user) for _ in range(3)]
    return {"user": user, "analyses": analyses}

@pytest.fixture
def completed_workflow(user_with_analyses):
    for analysis in user_with_analyses["analyses"]:
        analysis.status = "completed"
    return user_with_analyses
python
@pytest.fixture
def user():
    return UserFactory()

@pytest.fixture
def user_with_analyses(user):
    analyses = [AnalysisFactory(user=user) for _ in range(3)]
    return {"user": user, "analyses": analyses}

@pytest.fixture
def completed_workflow(user_with_analyses):
    for analysis in user_with_analyses["analyses"]:
        analysis.status = "completed"
    return user_with_analyses

Test Data Isolation

测试数据隔离

python
@pytest.fixture(autouse=True)
async def clean_database(db_session):
    """Reset database between tests."""
    yield

    # Clean up after test
    await db_session.execute("TRUNCATE users, analyses CASCADE")
    await db_session.commit()
python
@pytest.fixture(autouse=True)
async def clean_database(db_session):
    """Reset database between tests."""
    yield

    # Clean up after test
    await db_session.execute("TRUNCATE users, analyses CASCADE")
    await db_session.commit()

Key Decisions

关键决策

DecisionRecommendation
StrategyFactories over fixtures
FakerUse for realistic random data
ScopeFunction-scoped for isolation
CleanupAlways reset between tests
决策建议
策略优先使用工厂而非Fixture
Faker用于生成真实感随机数据
作用域函数级作用域以保证隔离性
清理测试间始终重置数据

Common Mistakes

常见错误

  • Shared state between tests
  • Hard-coded IDs (conflicts)
  • No cleanup after tests
  • Over-complex fixtures
  • 测试间共享状态
  • 硬编码ID(易冲突)
  • 测试后未清理数据
  • Fixture过于复杂

Related Skills

相关技能

  • unit-testing
    - Test patterns
  • integration-testing
    - Database tests
  • database-schema-designer
    - Schema design
  • unit-testing
    - 测试模式
  • integration-testing
    - 数据库测试
  • database-schema-designer
    - 数据库设计

Capability Details

能力详情

fixture-generation

fixture-generation

Keywords: fixture, test fixture, pytest fixture, conftest Solves:
  • Create reusable test fixtures
  • Implement fixture composition
  • Handle fixture cleanup
关键词: fixture, test fixture, pytest fixture, conftest 解决问题:
  • 创建可复用的测试Fixture
  • 实现Fixture组合
  • 处理Fixture清理

factory-patterns

factory-patterns

Keywords: factory, FactoryBoy, test factory, model factory Solves:
  • Generate test data with factories
  • Implement factory inheritance
  • Create related object graphs
关键词: factory, FactoryBoy, test factory, model factory 解决问题:
  • 用工厂生成测试数据
  • 实现工厂继承
  • 创建关联对象图

data-seeding

data-seeding

Keywords: seed, seed data, database seed, initial data Solves:
  • Seed databases for testing
  • Create consistent test environments
  • Implement idempotent seeding
关键词: seed, seed data, database seed, initial data 解决问题:
  • 为测试填充数据库
  • 创建一致的测试环境
  • 实现幂等性数据填充

cleanup-strategies

cleanup-strategies

Keywords: cleanup, teardown, reset, isolation Solves:
  • Clean up test data after runs
  • Implement transaction rollback
  • Ensure test isolation
关键词: cleanup, teardown, reset, isolation 解决问题:
  • 测试运行后清理测试数据
  • 实现事务回滚
  • 保证测试隔离性

data-anonymization

data-anonymization

Keywords: anonymize, faker, synthetic data, mock data Solves:
  • Generate realistic fake data
  • Anonymize production data for tests
  • Create synthetic datasets
关键词: anonymize, faker, synthetic data, mock data 解决问题:
  • 生成真实感虚假数据
  • 对生产数据进行匿名化处理用于测试
  • 创建合成数据集