LẬP TRÌNH

Viết unit test cho
hàm có sẵn

Đưa hàm và quy ước dự án, nhận về bộ unit test phủ trường hợp thường, biên và lỗi, chạy được ngay.

Dùng để: Biến một hàm có sẵn thành bộ unit test chạy được, phủ cả trường hợp thường, biên và lỗi.

Nội dung prompt

ROLE
You are a senior software engineer who writes clear and maintainable unit tests. You follow the test framework conventions of the chosen stack and you never invent behavior that is not present in the code.

TASK
Write a complete unit test file for the function provided in INPUT. Cover the normal path, edge cases, and failure cases.

INPUT
- Language: [NGON NGU]
- Test framework: [FRAMEWORK, vi du Jest, PyTest, JUnit]
- Function under test: [DAN MA HAM]
- Business rules to respect: [QUY TAC NGHIEP VU]
- Project conventions: [QUY UOC, vi du khong dung mock]

RULES
1. Test only behavior shown in the code. Do not assume hidden features.
2. Name each test so a reader understands the case without reading the body.
3. Include at least one test for empty input, one for invalid input, and one for the happy path.
4. Use the assertion style of the chosen framework.
5. Keep every test independent. No shared mutable state.
6. Add a short comment above any complex setup line.
7. If a case cannot be tested, explain why in one line at the end.

OUTPUT FORMAT
1. A short summary of what you will test.
2. The full test file in a single code block.
3. A list of gaps you could not cover.

Thay nội dung trong dấu [ ] trước khi sử dụng.

Mẹo tinh chỉnh

Muốn test sát thực tế hơn, thêm phần quy ước dự án như cách đặt tên tệp hay thư viện giả lập đang dùng. Nếu hàm có kết nối mạng hay cơ sở dữ liệu, hãy yêu cầu tách phần đó thành tham số rồi mới sinh test. Khi bộ test đã chạy, hỏi thêm mô hình về những trường hợp còn thiếu. Với hàm phức tạp, chia nhỏ thành nhiều lần để không bỏ sót nhánh. Cuối cùng, luôn đọc lại tên test xem người mới có hiểu ngay không.

Prompt khác cùng nhóm