เขียน Prompt ให้ AI สร้าง Test Cases ที่ใช้ได้จริง
บทนี้สอนวิธี prompt ให้ AI gen test case ที่ใกล้ใช้งานจริง ไม่ใช่ดูดีลอย ๆ พร้อมโครงสร้าง prompt ที่ใช้ซ้ำได้กับงานทุกประเภท
จบบทนี้แล้วคุณจะ...
- เขียน prompt ที่ทำให้ AI gen test case ใกล้ใช้งานจริงได้
- ระบุ context ที่จำเป็นต้องใส่ใน prompt เพื่อลด hallucination
- ใช้ structured prompt template ที่ทำซ้ำได้กับ requirement ทุกแบบ
- รู้วิธี iterate prompt เมื่อ output แรกยังไม่ดีพอ
ปัญหาคือ context ที่ขาด ไม่ใช่ AI ไม่เก่ง
Prompt แบบไม่คิดมาก:
gen test case for login feature
จะได้ output ทั่วไปที่:
- ครอบคลุม happy path กับ wrong password
- ไม่รู้ว่าระบบใช้ email หรือ username
- ไม่รู้ business rule (เช่น lockout หลัง fail 3 ครั้ง)
- ไม่รู้ว่าทีมเขียน test case ในรูปแบบไหน
ผลคือ test case ที่ "ดูใช้ได้" แต่ไม่ตรงกับสถานการณ์จริง และ QA ต้องแก้เกือบทุกข้อ
5 ส่วนที่ควรอยู่ใน prompt ทุกครั้ง
| ส่วน | ทำไมต้องมี |
|---|---|
| Role | บอก AI ว่าตอบในมุมของใคร เช่น "QA engineer ที่มีประสบการณ์ 5 ปี" |
| Context | spec, business rule, data ที่เกี่ยวข้อง — ใส่ตรง ๆ ใน prompt |
| Task | บอกชัดว่าต้องการอะไร ในรูปแบบไหน จำนวนเท่าไหร่ |
| Constraints | สิ่งที่ห้ามทำ เช่น "อย่าใช้ method ที่ไม่มีใน spec" |
| Output format | ตาราง? markdown? JSON? Gherkin? |
ตัวอย่าง prompt ที่ใช้ทั้ง 5 ส่วน:
You are a senior QA engineer.
Context:
- Feature: User login with email + password
- Business rules:
- Email must be in valid format
- Password min 8 chars, must contain 1 number
- After 3 failed attempts in 5 min, account locks for 15 min
- Lockout email is sent on lock event
- Out of scope: SSO, password reset
Task: Write test cases covering happy path, validation, lockout, and lockout email.
Constraints:
- Only use rules listed in Context
- Mark test cases by priority (High / Medium / Low)
- Each test case = ID, Title, Preconditions, Steps, Expected
Output format: Markdown table.
Context มากเกินก็ทำให้ AI หลุดได้
หลักง่าย ๆ:
- Spec / requirement ที่เกี่ยวข้อง — ใส่หมด
- Business rule ที่ AI ต้องรู้ — list แบบ bullet
- ตัวอย่าง output จากทีมที่เคยใช้ — ใส่ 1-2 ตัวอย่างถ้าทำได้
- ข้อมูลที่ไม่เกี่ยว — อย่าใส่ จะทำให้ AI สับสน
ถ้า requirement มาในรูป Confluence หรือ Jira ยาว ๆ ให้สรุปก่อน:
- ลบ history, comment ที่ไม่เกี่ยวกับ test scope
- เก็บเฉพาะ acceptance criteria + business rule
- ระบุ explicit ว่าอะไร in scope / out of scope
ตัวอย่าง 1-2 ตัวที่ดี ดีกว่าคำอธิบายยาว
ถ้าทีมมี template test case อยู่แล้ว ใส่ตัวอย่าง 1 อันใน prompt:
Format test cases like this example:
ID: TC-LOGIN-001
Title: User can login with valid credentials
Priority: High
Preconditions:
- User account exists with email "[email protected]" and password "Pass1234"
Steps:
1. Navigate to /login
2. Enter email "[email protected]"
3. Enter password "Pass1234"
4. Click "Login" button
Expected:
- Redirect to /dashboard
- Welcome message shows user's name
จากนั้น AI จะ gen ตัวที่เหลือใน format เดียวกัน รวมถึง "tone" ของ wording ด้วย
ถ้าครั้งแรกไม่ดี อย่าเริ่มใหม่จากศูนย์
แทนที่จะเขียน prompt ใหม่ทั้งหมด ให้ตอบกลับสิ่งที่ขาดทีละจุด:
Good start. But:
- Add cases for SQL injection in email field
- Lockout case should also verify the lockout email content
- Remove cases that test password reset (out of scope)
- Combine the two "valid login" cases into one
วิธีนี้ AI จะเก็บ context เดิมไว้ ไม่ต้องเริ่มต้นใหม่
หลักการ iterate ที่ดี:
- บอกชัดว่าอันไหนใช้ได้, อันไหนไม่ใช้
- บอกเหตุผลด้วย ไม่ใช่แค่ "เปลี่ยน"
- ขยายทีละจุด อย่าเปลี่ยนทุกอย่างพร้อมกัน
copy-paste แล้วเติมส่วน [...] ของคุณ
You are a senior QA engineer working on [PRODUCT NAME].
Context:
- Feature: [FEATURE NAME]
- User story: [USER STORY]
- Acceptance criteria:
[LIST AC]
- Business rules:
[LIST RULES]
- Existing test cases nearby (for style reference):
[PASTE 1-2 EXAMPLES]
- Out of scope: [LIST]
Task: Write [N] test cases covering [happy path / validation / edge / negative / security].
Constraints:
- Only use rules listed above
- Mark priority (High / Medium / Low) with one-line reason
- Each test case must have a clear Expected that can fail
Output format: Markdown table with columns ID, Title, Priority, Preconditions, Steps, Expected.
แบบฝึก: เขียน prompt สำหรับ feature ของคุณ
เลือก feature ในงานจริงของคุณที่กำลังจะออก แล้วเขียน prompt โดยใช้ template ข้างบน เติมทุกช่อง [...] อย่างน้อย:
- 1 acceptance criterion
- 2 business rules
- 1 ตัวอย่าง test case ของทีม
- ระบุ scope ที่ in/out ชัดเจน
ดูคำตอบแนะนำ (เป้าหมายของแบบฝึก)
ไม่มี answer key ตายตัวเพราะแต่ละทีมต่างกัน แต่ prompt ของคุณควร:
- ยาวอย่างน้อย 15-20 บรรทัด
- มีทั้ง 5 ส่วน (role, context, task, constraints, format)
- มีตัวอย่างอย่างน้อย 1 ตัว
- ระบุ "out of scope" ชัดเจน
หลังเขียน prompt เสร็จ ลองรัน 2 รอบ: ด้วย prompt แบบสั้น (1 บรรทัด) กับ prompt ที่คุณเขียน เปรียบเทียบ output แล้วบันทึกความต่าง
สิ่งที่ต้องเอาติดตัวไปจากบทนี้
- prompt ที่ดีต้องมี role, context, task, constraints, output format
- Context ที่ขาดคือสาเหตุหลักของ output ที่ "ดูดีแต่ใช้ไม่ได้"
- ใช้ few-shot ใส่ตัวอย่าง 1-2 อันเพื่อ lock format กับ style
- iterate prompt โดยตอบกลับเฉพาะจุดที่ต้องแก้ อย่าเริ่มใหม่
- มี template ตัวเดียวที่ใช้ซ้ำได้ ทำให้ทีมทั้งทีมใช้แนวเดียวกัน