I was writing tests using ginkgo framework, and wanted to reduce duplication within my tests. Suppose I have two tests, which have a exactly common middle section, but different start and end sections. ginkgo provides BeforeEach
function to remove duplication from the start, but I couldn't find any syntax to just define a simple utility function within Describe
node. The best I could think of was assigning a function to a variable, but variable initialization is not allowed in container nodes.

- 19
- 5
1 Answers
I am not completely sure what do you mean by syntax to write utility in describe node. If you go through their documentation description, context etc are container nodes and are just sugarcoating to better manage test descriptions and readability. we cannot hold codes in those container nodes. The only code that executes is inside ginkgo.specify
refer this link: https://onsi.github.io/ginkgo/#adding-specs-to-a-suite
Now, to solve your problem, it's basically a test design issue and It totally depends on how you design your test cases. You can simply introduce fixtures files for test data/reusable functions. So for example we have a structure like this:
Testsuite:
|- a_runnertest.go - only controls spec runs
|- b_case.go - handles cases
|- c_fixture.go - handles all reusable functions and test data
now for any functions which are reusable and wanted to be used across various describes, we move that code to fixture and call it in b_case.go. it will also be scalable moving forward.

- 21
- 1
- 9