tables-and-dynamic-specs
Skill này làm gì
Skill này chỉ cách viết một mẫu kiểm thử Ginkgo chạy với nhiều bộ giá trị đầu vào khác nhau. Nó dành cho lập trình viên có nhiều bài kiểm thử Go tương tự nhau.
Khi nào nên dùng
- ✓Khi bạn có nhiều bài kiểm thử Go tương tự, chỉ khác nhau ở giá trị đầu vào.
- ✓Khi bạn muốn biến một danh sách các trường hợp kiểm thử thành các bài kiểm thử Ginkgo riêng biệt.
- ✓Khi bạn cần nạp dữ liệu kiểm thử từ file trước khi một bộ kiểm thử Ginkgo bắt đầu.
Liên kết skill
https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/tables-and-dynamic-specs/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Tôi có 20 bài kiểm thử Go tương tự với đầu vào khác nhau. Bạn có thể gộp chúng thành một bài kiểm thử Ginkgo kiểu bảng không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill dùng một bảng trong đó mỗi hàng là một trường hợp kiểm thử với đầu vào và tên riêng, nên code kiểm thử chỉ phải viết một lần.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Table specs and dynamically generated specs
Ginkgo gives you a DSL for table-driven specs plus idioms for generating specs from loops and data. All of it is syntactic sugar that runs during the tree-construction phase (ginkgo:overview) — the gotchas all follow from that.
Perfer DescrtibeTable/DescribeTableSubtree with shared configuration over multiple repeated Its.
Docs: https://onsi.github.io/ginkgo/#table-specs.
DescribeTable + Entry
DescribeTable(desc, specFunc, ...Entry) generates one container holding one It per Entry. Entry(desc, params...) — its params are passed to specFunc at run time and must match specFunc's signature (you get a clear runtime message if they don't).
DescribeTable("Extracting the author's first and last name",
func(author string, isValid bool, firstName, lastName string) {
book := &books.Book{Title: "My Book", Author: author, Pages: 10}
Expect(book.IsValid()).To(Equal(isValid))
Expect(book.AuthorFirstName()).To(Equal(firstName))
Expect(book.AuthorLastName()).To(Equal(lastName))
},
Entry("both names", "Victor Hugo", true, "Victor", "Hugo"),
Entry("one name", "Hugo", true, "", "Hugo"),
Entry("no name", "", false, "", ""),
)
A DescribeTable is just a container, so nest it inside Describe/Context and surround it with BeforeEach — setup runs fresh before each entry's spec.
THE gotcha: Entry params are evaluated at construction time
Entry(...) arguments are evaluated when the tree is built — before any BeforeEach has run. So an Entry cannot read a variable initialized in BeforeEach; it will see the zero value (a nil map/pointer).
var shelf map[string]*books.Book
BeforeEach(func() { shelf = loadShelf() }) // runs at RUN time
// WRONG — shelf is nil when Entry is evaluated at construction time
DescribeTable("category", func(b *books.Book, c books.Category) { ... },
Entry("novel", shelf["Les Miserables"], books.CategoryNovel), // nil pointer!
)
// RIGHT — pass a key, dereference shelf inside the spec closure (run time)
DescribeTable("category", func(key string, c books.Category) {
Expect(shelf[key].Category()).To(Equal(c))
},
Entry("novel", "Les Miserables", books.CategoryNovel),
)
The four ways to describe an Entry
| Mechanism | How |
|---|---|
| Explicit string | Entry("both names", ...) |
nil |
Entry(nil, 1, 2, 3) → auto-named from params: Entry: 1, 2, 3 |
| Table-level description closure | pass func(a,b,c int) string {...} as 3rd arg to DescribeTable; renders every nil entry |
EntryDescription(fmt) |
EntryDescription("%d + %d = %d") as 3rd arg to DescribeTable, or per-entry |
The description closure must return string and accept the same params as specFunc. Per-entry, the first arg may itself be a closure or EntryDescription (overrides the table default):
DescribeTable("addition", func(a, b, c int) { Expect(a + b).To(Equal(c)) },
EntryDescription("%d + %d = %d"), // table default
Entry(nil, 1, 2, 3), // "1 + 2 = 3"
Entry("zeros", 0, 0, 0), // explicit
Entry(EntryDescription("%[3]d = %[1]d + %[2]d"), 10, 100, 110), // per-entry override
Entry(func(a, b, c int) string { return fmt.Sprintf("%d = %d", a+b, c) }, 4, 3, 7),
)
Decorating entries
Entry and DescribeTable accept every decorator (→ ginkgo:decorators): Entry("flaky case", FlakeAttempts(3), ...), Entry(..., Label("slow")). Focus/pending shortcuts: FEntry / PEntry / XEntry (and FDescribeTable / PDescribeTable). PEntry needs no params; focus/pending obey the same precedence as everywhere else (→ ginkgo:filtering).
DescribeTableSubtree — many Its per row
When you want a whole subtree (multiple Its, their own setup) per entry, use DescribeTableSubtree. Its body runs at construction time, once per entry, inside a fresh container — you must place Its inside it or no specs are generated:
DescribeTableSubtree("handling requests",
func(url string, code int, message string) {
var resp *http.Response
BeforeEach(func() {
var err error
resp, err = http.Get(url)
Expect(err).NotTo(HaveOccurred())
DeferCleanup(resp.Body.Close)
})
It("returns the status code", func() { Expect(resp.StatusCode).To(Equal(code)) })
It("returns the message", func() {
body, _ := io.ReadAll(resp.Body)
Expect(string(body)).To(E
Câu hỏi thường gặp
Vì sao tên các hàng trong bảng kiểm thử của tôi không hoạt động?
Skill giải thích rằng đầu vào của mỗi hàng phải khớp với đầu vào của hàm kiểm thử, và đưa ra mẹo đặt tên cho các hàng.
Tôi có thể chỉ chạy một số hàng của bảng không?
Có. Skill bao gồm cách đánh dấu các hàng để tập trung hoặc bỏ qua khi bạn đang làm việc.
Cách này có dễ bảo trì hơn các bài kiểm thử riêng lẻ không?
Có. Phần thiết lập chung được giữ ở một chỗ, nên thay đổi chỉ cần làm một lần thay vì nhiều lần.
Chi tiết
Mô tả gốc của tác giả
Parameterize and generate Ginkgo specs — DescribeTable/Entry table-driven specs, Entry descriptions (string, nil, closure, EntryDescription), PEntry/FEntry and per-Entry decorators, DescribeTableSubtree, generating specs in a construction-time loop, loading fixtures in TestXxx before RunSpecs, and shared-behavior closures. Use when you have repetitive specs differing only by inputs, want data-driven or generated specs, or are extracting reusable It blocks across Contexts.
repo: onsi/ginkgo
Độ phổ biến
- Sao GitHub
- 9.1k
- Vị trí xếp hạng
- #12308
- Giấy phép
- MIT
- Cập nhật
- 9/10/2026
Bình luận
- …
Thêm skill từ kho mã này
ci
9.1kGiúp agent của bạn thiết lập kiểm thử Ginkgo cho CI, với các cờ khuyến nghị để bắt dấu focus hoặc pending còn sót và bài kiểm thử không ổn định.
debugging-failures
9.1kGiúp agent của bạn tìm lý do kiểm thử Ginkgo bị lỗi bằng cách đọc dòng tóm tắt và lấy đúng các bài thất bại cùng thông báo từ báo cáo JSON.
decorators
9.1kGiúp agent của bạn dùng đúng các decorator của kiểm thử Ginkgo, như Serial, Ordered, Label, Focus và timeout, bằng cách giải thích mỗi decorator làm gì.
filtering
9.1kGiúp agent của bạn chỉ chạy một số bài kiểm thử Ginkgo, dùng pending, skip, focus, label, ràng buộc phiên bản và bộ lọc file theo đúng thứ tự ưu tiên.
Skill tương tự
test-driven-development
311kDạy agent của bạn viết kiểm thử thất bại trước, xem nó thất bại, rồi viết đoạn mã ngắn nhất để vượt qua trước khi làm bất kỳ tính năng hay bản sửa nào.
verification-before-completion
311kDạy agent của bạn chạy kiểm tra thực tế và đọc kết quả đầu ra trước khi khẳng định một việc, bản sửa hoặc bản build đã hoàn thành hay vượt qua.
migrate-to-shoehorn
310kThay các ép kiểu rủi ro trong tệp kiểm thử bằng thư viện shoehorn, để các bài kiểm thử dùng dữ liệu một phần vẫn qua được kiểm tra kiểu.