writing-specs
Skill này làm gì
Skill này chỉ cách viết các bài kiểm thử Ginkgo có tổ chức tốt trong Go. Nó dành cho lập trình viên viết hoặc rà soát kiểm thử Go với Ginkgo.
Khi nào nên dùng
- ✓Khi bạn viết các bài kiểm thử Ginkgo mới cho một package Go.
- ✓Khi các bài kiểm thử Ginkgo của bạn chia sẻ dữ liệu và bắt đầu làm nhiễu nhau.
- ✓Khi bạn muốn rà soát hoặc dọn dẹp code kiểm thử Ginkgo hiện có.
Liên kết skill
https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/writing-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
“Viết các bài kiểm thử Ginkgo cho code kệ sách Go của tôi, với phần thiết lập sạch cho mỗi bài kiểm thử.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill giải thích các phần của một bài kiểm thử: các nhóm để tổ chức bài kiểm thử, bản thân bài kiểm thử, và code thiết lập chạy mới trước mỗi bài.
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.
Writing Ginkgo specs
Assumes Ginkgo is wired into the suite (ginkgo:setup) and you know the two-phase model (ginkgo:overview). Docs: https://onsi.github.io/ginkgo/#writing-specs.
The shape of a spec
var _ = Describe("Books", func() {
var book *books.Book // declare here...
BeforeEach(func() {
book = &books.Book{ // ...initialize here, fresh per spec
Title: "Les Miserables",
Author: "Victor Hugo",
Pages: 2783,
}
Expect(book.IsValid()).To(BeTrue()) // assertions in setup are fine
})
Describe("categorizing", func() {
Context("with more than 300 pages", func() {
It("is a novel", func() {
Expect(book.Category()).To(Equal(books.CategoryNovel))
})
})
})
})
- Containers (
Describe/Context/When) organize; they're identical — pick the one that reads as a sentence. Subjects (It/Specify) hold the assertions; one spec runs perIt. - The spec's full name is the concatenation of every container text plus the
Ittext — write them to read as a phrase.
The rule that prevents most bugs: declare in container, initialize in setup
Container bodies run once, at tree-construction time (ginkgo:overview). So:
- Declare shared variables in the container body (
var book *books.Book). - Initialize them in
BeforeEachso each spec gets a clean copy.
// WRONG — runs once at construction; every spec shares & pollutes one book
var _ = Describe("Books", func() {
book := &books.Book{Pages: 2783} // initialized at construction time
It("mutates", func() { book.Pages = 0 })
It("expects 2783", func() { Expect(book.Pages).To(Equal(2783)) }) // flaky under randomization
})
The same trap explains: no assertions in container bodies (they fire at construction, with no spec active) and no expensive/stateful work in container bodies. If you see logic directly inside a Describe/Context/When body, it almost always belongs in a BeforeEach.
Setup and cleanup nodes — and their ordering
| Node | Runs |
|---|---|
BeforeEach |
Before each spec, outer→inner. The workhorse. |
JustBeforeEach |
After all BeforeEaches, just before the It. |
JustAfterEach |
Just after the It, before any AfterEach. |
AfterEach |
After each spec, inner→outer (reverse). |
BeforeSuite/AfterSuite |
Once, around the whole suite (top-level only). |
BeforeAll/AfterAll |
Once per Ordered container (→ ginkgo:ordering-and-flakes). |
JustBeforeEach separates creation from configuration. Let nested BeforeEaches configure inputs into declared variables, and do the single creation step in JustBeforeEach — so each context overrides just the inputs it cares about:
var jsonString string
BeforeEach(func() { jsonString = `{"id":1,"name":"Sally"}` }) // base config
JustBeforeEach(func() { user, err = NewUser(jsonString) }) // creation, runs last
Context("with malformed JSON", func() {
BeforeEach(func() { jsonString = `{"oops"` }) // override one input
It("errors", func() { Expect(err).To(HaveOccurred()) })
})
Use it deliberately — deeply nested JustBeforeEaches get hard to follow.
Cleanup: prefer DeferCleanup, and restore rather than clear
DeferCleanup registers teardown next to the setup that needs it, and runs in LIFO order (like defer). It works in any setup/subject node and adapts to scope (called in BeforeSuite, it cleans up after the suite; in BeforeEach, after the spec):
BeforeEach(func() {
original := os.Getenv("MODE")
os.Setenv("MODE", "test")
DeferCleanup(os.Setenv, "MODE", original) // captured args passed at cleanup time
})
DeferCleanupacceptsfunc(),func() error(a non-nil error fails the spec), captured arguments, and afunc(ctx SpecContext)form (→ginkgo:timeouts-and-async).- Restore original state; don't blindly clear it.
os.Unsetenvafter the test wrongly assumes the var started unset — capture and restore instead (as above). DeferCleanupis a function call, not a node — it's the one cleanup mechanism you may use inside setup/subject closures. You may not define nodes (It,BeforeEach, …) inside a running closure.
Capturing failure state: DeferOnFailure
DeferOnFailure registers diagnostics that run only if the spec fails, as soon as the failing node returns and before any teardown nodes — so they see the state that AfterEach/DeferCleanup are about t
Câu hỏi thường gặp
Tôi nên đặt biến cho bài kiểm thử ở đâu?
Khai báo chúng trong nhóm và gán giá trị trong bước thiết lập để mỗi bài kiểm thử bắt đầu sạch.
DeferCleanup là gì?
Nó lên lịch một bước dọn dẹp chạy sau bài kiểm thử, ngay cả khi bài kiểm thử thất bại.
Tôi có thể tạo các hàm hỗ trợ cho kiểm thử không?
Có. Skill bao gồm các hàm hỗ trợ có thể tái sử dụng và báo lỗi đúng dòng.
Chi tiết
Mô tả gốc của tác giả
Author good Ginkgo specs — container nodes (Describe/Context/When), subject nodes (It/Specify), setup/cleanup nodes (BeforeEach, JustBeforeEach, AfterEach, DeferCleanup, BeforeSuite/AfterSuite), capturing failure diagnostics before teardown with DeferOnFailure, the "declare in container, initialize in BeforeEach" rule, separating creation from configuration, reusable test helpers with GinkgoHelper()/GinkgoHelperGo(), and By/GinkgoWriter output. Use when writing or reviewing specs or extracting a test helper. Covers the tree-construction-time pitfalls (no assertions/init/loop-capture in contain
repo: onsi/ginkgo
Độ phổ biến
- Sao GitHub
- 9.1k
- Vị trí xếp hạng
- #12310
- 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.