writing-specs
यह क्या करती है
यह स्किल दिखाती है कि Go में व्यवस्थित Ginkgo टेस्ट कैसे लिखें। यह उन डेवलपर्स के लिए है जो Ginkgo से Go टेस्ट लिखते या समीक्षा करते हैं।
कब इस्तेमाल करें
- ✓जब आप किसी Go पैकेज के लिए नए Ginkgo टेस्ट लिख रहे हों।
- ✓जब आपके Ginkgo टेस्ट डेटा साझा करते हों और एक-दूसरे में दखल देने लगें।
- ✓जब आप मौजूदा Ginkgo टेस्ट कोड की समीक्षा या सफ़ाई करना चाहते हों।
Skill का लिंक
https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/writing-specs/SKILL.mdइसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।
कैसे इस्तेमाल करें
सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।
पेस्ट करने के लिए तैयार मैसेज
Claude Code, Codex या Cursor के लिए: प्रोजेक्ट फ़ोल्डर में चलाएँ और skill इंस्टॉल हो जाएगी।
SKILL.md डाउनलोड करें और अपने एजेंट के skills फ़ोल्डर में रखें।
AI से क्या माँगें: उदाहरण
“मेरे Go bookshelf कोड के लिए Ginkgo टेस्ट लिखिए, और हर टेस्ट के लिए साफ़ सेटअप रखिए।”
हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।
यह कैसे काम करती है
यह टेस्ट के हिस्से समझाती है: समूह जो टेस्ट को व्यवस्थित करते हैं, टेस्ट खुद, और सेटअप कोड जो हर टेस्ट से पहले नए सिरे से चलता है।
तकनीकी सामग्री (मूल SKILL.md)
लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।
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
अक्सर पूछे जाने वाले सवाल
अपने टेस्ट के variables कहां रखूं?
उन्हें समूह में घोषित करें और मान सेटअप चरण में दें, ताकि हर टेस्ट साफ़ शुरुआत करे।
DeferCleanup क्या है?
यह एक cleanup चरण तय करता है जो टेस्ट के बाद चलता है, भले ही टेस्ट फ़ेल हो जाए।
क्या टेस्ट के लिए helper functions बना सकते हैं?
हाँ, स्किल दोबारा इस्तेमाल होने वाले helper functions कवर करती है, जो सही लाइन पर विफलता बताते हैं।
जानकारी
लेखक का मूल विवरण
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
लोकप्रियता
- GitHub स्टार
- 9.1k
- रैंकिंग में स्थान
- #12310
- लाइसेंस
- MIT
- अपडेट की गई
- 9/10/2026
कमेंट
- …
इस repository की और skills
ci
9.1kयह आपके एजेंट को Ginkgo टेस्ट CI के लिए सेट करने में मदद करती है, ताकि बचे हुए focus या pending निशान और flaky टेस्ट पकड़े जा सकें।
debugging-failures
9.1kयह आपके एजेंट को summary लाइन पढ़कर और JSON रिपोर्ट से ठीक फ़ेल होने वाले टेस्ट और संदेश निकालकर Ginkgo टेस्ट फ़ेल होने का कारण ढूंढने में मदद करती है।
decorators
9.1kयह आपके एजेंट को Ginkgo टेस्ट decorators सही ढंग से इस्तेमाल करने में मदद करती है, जैसे Serial, Ordered, Label, Focus और timeouts, हर एक का काम समझाकर।
filtering
9.1kयह आपके एजेंट को Ginkgo के कुछ ही टेस्ट चलाने में मदद करती है, pending, skip, focus, labels और file filters को सही क्रम में लगाकर।
मिलती-जुलती skills
test-driven-development
311kयह आपके एजेंट को किसी भी फ़ीचर या सुधार से पहले पहले असफल होने वाला टेस्ट लिखना, उसे असफल होते देखना, फिर पास करने वाला सबसे छोटा कोड लिखना सिखाती है।
verification-before-completion
311kयह आपके एजेंट को किसी काम, सुधार या build के पूरा या पास होने का दावा करने से पहले असली जांच चलाकर उसका आउटपुट पढ़ना सिखाती है।
migrate-to-shoehorn
310kआपकी टेस्ट फ़ाइलों में जोखिम भरे type casts की जगह shoehorn लाइब्रेरी लगाती है, ताकि टेस्ट अधूरे डेटा से भी type checks पास कर सकें।