mcpField
Q
qe-test-data-management

qe-test-data-management

github.com/proffesor-for-testing测试与质量51981 次安装今天更新
代码仓库

关于 qe-test-data-management

面向测试工程师和开发者的测试数据管理技能,涵盖合成数据生成、敏感信息匿名化与数据隔离。内容还介绍批量生成和按测试类型选择数据规模。

主要功能

  1. 01使用 faker 生成可复现的合成数据
  2. 02通过掩码或哈希处理敏感数据
  3. 03使用事务隔离测试数据并清理
  4. 04按批次生成大量数据
  5. 05为测试数据构建器和关系约束提供示例

适用场景

  1. 01为单元测试创建最少量数据
  2. 02为集成测试准备真实结构的数据
  3. 03生成性能测试所需的大批量数据
  4. 04避免测试环境直接使用生产个人信息

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/proffesor-for-testing/agentic-qe --skill qe-test-data-management

安装后在 Claude Code 中输入/qe-test-data-management确认已加载。

详细介绍

测试数据管理

<default_to_action> 创建或管理测试数据时:

  1. 绝不要直接使用生产环境中的个人身份信息(PII)
  2. 使用 faker 库生成合成数据
  3. 如需使用生产数据,请先匿名化(遮蔽、哈希)
  4. 隔离测试数据(使用事务、每次测试后清理)
  5. 通过批量生成实现扩展(每秒 10k+ 条记录)

快速数据策略:

  • 单元测试:最少数据(满足需求即可)
  • 集成测试:逼真数据(包含完整复杂性)
  • 性能测试:大量数据(10k+ 条记录)

关键成功因素:

  • 40% 的测试失败源于数据不足
  • 违反 PII 相关规定,GDPR 罚款最高可达 €20M
  • 绝不要在测试环境中存储生产 PII </default_to_action>

快速参考卡

适用场景
  • 创建测试数据集
  • 处理敏感数据
  • 使用大量数据进行性能测试
  • 确保符合 GDPR/CCPA
数据策略
类型适用场景数量
最少量单元测试1-10 条记录
逼真数据集成测试100-1000 条记录
大量数据性能测试10k+ 条记录
边界情况边界测试针对性数据
隐私保护技术
技术用途
合成数据生成虚假数据(首选)
遮蔽j***@example.com
哈希不可逆的假名化
令牌化可通过密钥还原

合成数据生成

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

// Seed for reproducibility
faker.seed(123);

function generateUser() {
  return {
    id: faker.string.uuid(),
    email: faker.internet.email(),
    firstName: faker.person.firstName(),
    lastName: faker.person.lastName(),
    phone: faker.phone.number(),
    address: {
      street: faker.location.streetAddress(),
      city: faker.location.city(),
      zip: faker.location.zipCode()
    },
    createdAt: faker.date.past()
  };
}

// Generate 1000 users
const users = Array.from({ length: 1000 }, generateUser);

测试数据构建器模式

class UserBuilder {
  private user: Partial<User> = {};

  asAdmin() {
    this.user.role = 'admin';
    this.user.permissions = ['read', 'write', 'delete'];
    return this;
  }

  asCustomer() {
    this.user.role = 'customer';
    this.user.permissions = ['read'];
    return this;
  }

  withEmail(email: string) {
    this.user.email = email;
    return this;
  }

  build(): User {
    return {
      id: this.user.id ?? faker.string.uuid(),
      email: this.user.email ?? faker.internet.email(),
      role: this.user.role ?? 'customer',
      ...this.user
    } as User;
  }
}

// Usage
const admin = new UserBuilder().asAdmin().withEmail('[email protected]').build();
const customer = new UserBuilder().asCustomer().build();

数据匿名化

// Masking
function maskEmail(email) {
  const [user, domain] = email.split('@');
  return `${user[0]}***@${domain}`;
}
// [email protected] → j***@example.com

function maskCreditCard(cc) {
  return `****-****-****-${cc.slice(-4)}`;
}
// 4242424242424242 → ****-****-****-4242

// Anonymize production data
const anonymizedUsers = prodUsers.map(user => ({
  id: user.id, // Keep ID for relationships
  email: `user-${user.id}@example.com`, // Fake email
  firstName: faker.person.firstName(), // Generated
  phone: null, // Remove PII
  createdAt: user.createdAt // Keep non-PII
}));

数据库事务隔离

// Best practice: use transactions for cleanup
beforeEach(async () => {
  await db.beginTransaction();
});

afterEach(async () => {
  await db.rollbackTransaction(); // Auto cleanup!
});

test('user registration', async () => {
  const user = await userService.register({
    email: '[email protected]'
  });
  expect(user.id).toBeDefined();
  // Automatic rollback after test - no cleanup needed
});

大量数据生成

// Generate 10,000 users efficiently
async function generateLargeDataset(count = 10000) {
  const batchSize = 1000;
  const batches = Math.ceil(count / batchSize);

  for (let i = 0; i < batches; i++) {
    const users = Array.from({ length: batchSize }, (_, index) => ({
      id: i * batchSize + index,
      email: `user${i * batchSize + index}@example.com`,
      firstName: faker.person.firstName()
    }));

    await db.users.insertMany(users); // Batch insert
    console.log(`Batch ${i + 1}/${batches}`);
  }
}

Agent 驱动的数据生成

// High-speed generation with constraints
await Task("Generate Test Data", {
  schema: 'ecommerce',
  count: { users: 10000, products: 500, orders: 5000 },
  preserveReferentialIntegrity: true,
  constraints: {
    age: { min: 18, max: 90 },
    roles: ['customer', 'admin']
  }
}, "qe-test-data-architect");

// GDPR-compliant anonymization
await Task("Anonymize Production Data", {
  source: 'production-snapshot',
  piiFields: ['email', 'phone', 'ssn'],
  method: 'pseudonymization',
  retainStructure: true
}, "qe-test-data-architect");

Agent 协作提示

记忆命名空间
aqe/test-data-management/
├── schemas/*            - Data schemas
├── generators/*         - Generator configs
├── anonymization/*      - PII handling rules
└── fixtures/*           - Reusable fixtures
集群协作
const dataFleet = await FleetManager.coordinate({
  strategy: 'test-data-generation',
  agents: [
    'qe-test-data-architect',  // Generate data
    'qe-test-executor',        // Execute with data
    'qe-security-scanner'      // Validate no PII exposure
  ],
  topology: 'sequential'
});

相关技能


请记住

测试数据是基础设施,不是事后才考虑的事项。 40% 的测试失败是由测试数据不足导致的。数据质量差 = 测试质量差。

绝不要直接使用生产 PII。 GDPR 罚款最高可达 €20M 或营业额的 4%。始终使用合成数据或经过妥善匿名化的生产数据快照。

使用 Agents: qe-test-data-architect 每秒可生成 10k+ 条记录,具有逼真的模式、关系和约束。Agents 可自动确保符合 GDPR/CCPA,并消除测试数据瓶颈。

评论

支持 Markdown