tables-and-dynamic-specs
Fungsinya
Skill ini menunjukkan cara menulis satu pola tes Ginkgo yang berjalan dengan banyak kumpulan nilai input berbeda. Skill ini ditujukan untuk pengembang yang punya banyak tes Go yang mirip.
Kapan dipakai
- โSaat Anda punya banyak tes Go yang mirip dan hanya berbeda pada nilai input.
- โSaat Anda ingin mengubah daftar kasus uji menjadi tes Ginkgo yang terpisah.
- โSaat Anda perlu memuat data tes dari file sebelum rangkaian Ginkgo dimulai.
Link skill
https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/tables-and-dynamic-specs/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
โSaya punya dua puluh tes Go yang mirip dengan input berbeda. Bisakah Anda menggabungkannya menjadi satu tes Ginkgo gaya tabel?โ
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
Skill memakai tabel di mana setiap baris adalah satu kasus uji dengan input dan namanya sendiri, sehingga kode tesnya hanya ditulis sekali.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.
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
Pertanyaan umum
Mengapa nama tes tabel saya tidak berfungsi?
Skill menjelaskan bahwa input setiap baris harus cocok dengan input fungsi tes, dan memberikan tips untuk memberi nama baris.
Bisakah saya menjalankan hanya sebagian baris tabel?
Bisa. Skill membahas cara menandai baris untuk difokuskan atau dilewati saat Anda bekerja.
Apakah ini lebih mudah dipelihara daripada tes terpisah?
Ya, karena pengaturan bersama disimpan di satu tempat, sehingga perubahan cukup dilakukan sekali, bukan berkali-kali.
Detail
Deskripsi asli dari pembuat
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
Popularitas
- Bintang GitHub
- 9.1k
- Peringkat
- #12308
- Lisensi
- MIT
- Diperbarui
- 9/10/2026
Komentar
- โฆ
Skill lain dari repositori ini
ci
9.1kMembantu agent Anda menyiapkan tes Ginkgo untuk integrasi berkelanjutan, dengan flag yang direkomendasikan untuk menangkap penanda fokus atau tertunda yang tertinggal serta tes yang tidak stabil.
debugging-failures
9.1kMembantu agent Anda mencari tahu mengapa tes Ginkgo gagal dengan membaca baris ringkasan dan mengambil tes yang gagal beserta pesannya dari laporan JSON.
decorators
9.1kMembantu agent Anda memakai dekorator tes Ginkgo dengan benar, seperti Serial, Ordered, Label, Focus, dan batas waktu, dengan menjelaskan fungsi masing-masing.
filtering
9.1kMembantu agent Anda menjalankan hanya sebagian tes Ginkgo, memakai pending, skip, focus, label, batasan versi, dan filter file sesuai urutan prioritasnya.
Skill serupa
test-driven-development
311kMengajari agent Anda menulis tes yang gagal dulu, melihatnya gagal, lalu menulis kode terkecil yang lulus, sebelum membangun fitur atau perbaikan apa pun.
verification-before-completion
311kMengajari agent Anda menjalankan pemeriksaan nyata dan membaca keluarannya sebelum menyatakan tugas, perbaikan, atau build sudah selesai atau lulus.
migrate-to-shoehorn
310kMengganti type cast berisiko di file tes Anda dengan pustaka shoehorn, sehingga tes bisa memakai data parsial dan tetap lulus pemeriksaan tipe.