CupCode.cc
Section 05 · Module 05 · Lesson 01 · ~20–24 นาที

สร้าง Test Data ที่สมจริงด้วย AI

บทนี้สอนใช้ AI gen test data ที่หลากหลาย ใกล้กับ production จริง พร้อมหลักการ mask PII และข้อควรระวังเมื่อทำงานกับ data ที่ดู real เกินไป

เวลาเรียน: 20–24 นาที ต้องรู้มาก่อน: Section 3 - Automation Thinking และพื้นฐาน test data รูปแบบ: อ่าน + ตัวอย่าง prompt + แบบฝึก gen data

จบบทนี้แล้วคุณจะ...

  • ใช้ AI gen test data ที่หลากหลายและใกล้ production จริง
  • รู้วิธีกำหนด schema และ constraint ใน prompt
  • เข้าใจการใช้ AI mask PII ในข้อมูล production จริง
  • หลีกเลี่ยงกับดักของ data ที่ "ดู realistic แต่ผิด"
1 · ทำไม test data เป็นปัญหา

Data ปลอมที่ใช้กันอยู่มัก "ปลอมเกินไป"

ปัญหา test data ที่พบบ่อย:

  • ใช้ John Doe, [email protected], 1234567890 ซ้ำทุก test
  • ไม่มี variation ของ edge case (ชื่อยาว, char พิเศษ, email format ต่าง)
  • ไม่ได้ test กับ data ที่ดูเหมือน production
  • bug ที่เกิดเฉพาะกับ data จริง (Unicode, locale, currency) ไม่ได้ถูกจับ

ผลลัพธ์: test ผ่านใน CI แต่พังใน production เพราะ data ในโลกจริงซับซ้อนกว่ามาก

2 · ใช้ AI gen data ที่หลากหลาย

Prompt ที่ระบุ schema + constraint ชัด

ตัวอย่าง prompt:

Generate 20 realistic user records as JSON for testing.

Schema:
- id: UUID v4
- email: valid email
- firstName: string
- lastName: string
- phone: E.164 format
- birthDate: YYYY-MM-DD, age 18-80
- country: ISO 3166-1 alpha-2 code
- preferredLanguage: BCP-47 tag

Variation requirements:
- Mix of countries (US, JP, TH, DE, BR — at least 3 each)
- Mix of name lengths (some 3 chars, some > 25 chars)
- Include 2 with non-Latin names (ไทย / 日本語)
- Include 1 with apostrophe in lastName (O'Brien)
- Include 1 with hyphenated lastName

Constraints:
- All emails must be unique
- No real person's data
- Phone numbers must NOT be reachable

Output: JSON array only, no commentary.

หลักสำคัญ:

  • ระบุ schema ชัด ไม่ปล่อยให้ AI ตีความ
  • ระบุ variation ที่อยากให้มี (ไม่งั้น AI จะ gen ซ้ำ ๆ)
  • ห้าม use real data เพื่อกัน privacy + reachability accident
3 · Edge case ที่มักลืมขอ

list สิ่งที่ AI จะไม่ gen ถ้าไม่ระบุ

Category ตัวอย่าง edge case
String length ชื่อ 1 char, name 100+ char, empty
Encoding Unicode emoji, RTL (Arabic, Hebrew), CJK
Date leap year (Feb 29), 1900-01-01, 2099-12-31, timezone DST
Number 0, negative, very large (max int), very small (epsilon)
Email + alias, dot-separated, IDN domain, very long local part
Phone extension, country code variation, unicode digits
Address PO Box, multiple lines, no zip, non-US format
Currency very small (0.01), negative, multi-currency in one order

ใส่ใน prompt:

Include edge cases:
- 1 record with emoji in firstName 🌸
- 1 with Arabic name (RTL)
- 1 with birthDate = 2000-02-29 (leap year)
- 1 with phone in international format with extension
- 1 with email using "+" alias
4 · ใช้ AI mask PII ของข้อมูล production

Use case จริงเมื่อต้อง test กับ data shape ของ prod

Workflow ที่ใช้กันบ่อย:

  1. ดึง production data set (ผ่าน sanitization process)
  2. ส่งให้ AI ระบุ field ที่เป็น PII
  3. AI replace ด้วย fake data ที่รักษา shape เดิม
  4. Verify ว่า data ที่ออกมาไม่ link กลับไป real person

ตัวอย่าง prompt:

The following is real production data. Mask all PII while preserving:
- Email format (length, domain TLD distribution)
- Phone format
- Address shape (city/state/zip relationship)
- Statistical distribution (e.g., 60% US, 20% UK, 20% other)

Replace with fake data that won't trace back to any real person.
Keep ID fields, timestamps, non-PII fields unchanged.

Input:
[PASTE DATA]

ข้อควรระวัง:

  • AI ไม่ควรเห็น production data ที่ sensitive จริง — ใช้ on-prem model หรือ enterprise tier ที่ไม่ log
  • Verify mask แล้ว 100% ก่อน export ออกจาก secure environment
  • มี pipeline auto-mask เพื่อไม่ให้ขึ้นกับ manual
5 · กับดัก: data ที่ "ดู realistic แต่ผิด"

บาง output ของ AI เป็นปัญหาแอบแฝง

Plausible-but-fake reference:

  • Phone number ที่ format ถูก แต่เป็นเบอร์จริงของคน
  • Email domain ที่มีอยู่จริง — อาจส่ง spam จริง
  • Address ที่ถูก format แต่ตรงกับบ้านจริง

วิธีกัน:

  • ใช้ reserved range สำหรับ test phone (555- ใน US, 070- พิเศษ)
  • ใช้ domain example.com, example.org, example.net (RFC reserved)
  • random address ผ่าน library ไม่ใช่ AI gen ตรง

Statistical artifact:

AI gen 100 user มักมี gender ratio ตาม training bias, name distribution ไม่ตรงประชากรจริง

วิธีกัน:

  • ระบุ distribution ใน prompt ("50% female, 50% male, mix อาเซียน 30%, ยุโรป 30%, ...")
  • หลัง gen verify distribution ก่อนใช้

Repeated pattern:

Gen 100 user ครั้งเดียว AI มักให้ pattern เดียว (ชื่อขึ้นต้นด้วย "A", email format เดียวกัน)

วิธีกัน:

  • gen หลาย batch แล้ว shuffle
  • ระบุ "ห้ามมีชื่อขึ้นต้นด้วยตัวอักษรเดียวกันเกิน 3 record"
6 · เปรียบเทียบ AI vs library (Faker, etc.)

ใช้ผสมกันได้ดีที่สุด

มิติ AI Faker (library)
ความเร็ว ช้ากว่า, ต้อง API call เร็วมาก, in-memory
ความหลากหลาย สูง, ปรับ prompt ได้ ตาม seed pattern
Edge case ระบุได้ละเอียด ต้อง code เอง
Locale support ภาษาธรรมชาติทุกภาษา จำกัดที่ provider
Cost $/run $0 หลังติดตั้ง
Determinism ไม่ deterministic seed-based reproducible

ใช้ผสม: Faker สำหรับ data ส่วนใหญ่, AI สำหรับ edge case + locale พิเศษ + statistical mask

Practice

แบบฝึก: gen test data set

เขียน prompt gen 10 product records สำหรับ e-commerce test ที่ต้องครอบคลุม:

  • ราคาตั้งแต่ $0.99 ถึง $9999.99
  • Currency: USD, EUR, JPY, THB
  • Name มีทั้งภาษาอังกฤษและไทย
  • 1 record มี discount 0%, 1 มี 50%, 1 มี 99%
  • Stock: out of stock 1, low stock (1-5) 2, normal stock 7
  • Description ยาวต่าง: 1 ประโยค, 1 paragraph, 1 มี markdown
  • 1 มี image URL ที่ broken (404)
ดูคำตอบแนะนำ (โครง prompt)
Generate 10 product records as JSON for e-commerce testing.

Schema:
- id: UUID v4
- name: string (mix English and Thai)
- description: string
- price: number (between 0.99 and 9999.99)
- currency: ISO 4217 (USD, EUR, JPY, THB)
- discount: percentage 0-99
- stock: integer 0-100
- imageUrl: string

Required variations:
- Currency: at least 2 USD, 2 EUR, 2 JPY, 2 THB
- Names: at least 4 in Thai
- Discount: include exactly 1 with 0%, 1 with 50%, 1 with 99%
- Stock: 1 with stock=0, 2 with stock 1-5, 7 with stock > 10
- Description: 1 single sentence, 1 paragraph, 1 with markdown formatting
- imageUrl: 1 must point to a broken URL (404)

Constraints:
- Use example.com or placeholder image services
- No real product names from actual brands
- Realistic price distribution per currency

Output: JSON array only.

สิ่งที่ต้องเอาติดตัวไปจากบทนี้

  • AI gen test data หลากหลายกว่า library แต่ต้องระบุ schema + variation ชัด
  • Edge case (Unicode, leap year, RTL, very long string) ต้อง explicit
  • Mask PII ของ production ใช้ AI ได้ แต่ต้อง verify และใช้ secure tier
  • ระวัง plausible-but-fake reference (เบอร์จริง, domain จริง) — ใช้ reserved range
  • ผสม AI กับ Faker ดีกว่าใช้อย่างเดียว — ความเร็วรวมความหลากหลาย