returneax
returneax

Reputation: 709

Is it possible to have a shared context across unit tests in go test?

I am implementing a trie in Go as a way of learning the language. I want to write tests from the get go as a way of getting a feel for Go's approach to things.

One problem I am having when testing my trie is that I have to rebuild it for every unit test. Is there a way to reuse the same instantiation of my trie across unit tests? Ideally, I'd like a way to do this without any external dependencies.

Upvotes: 1

Views: 1531

Answers (2)

James Henstridge
James Henstridge

Reputation: 43909

If you don't mind the extra dependency, the gocheck package provides the ability to group tests into suites. You can then define SetUpSuite and TearDownSuite methods to perform initialisation and tear down of any resources shared by the tests in that suite.

Upvotes: 3

Volker
Volker

Reputation: 42413

Yes: Just construct it in func init() in your trie_test.go. (Or even use a literal.)

Upvotes: 4

Related Questions