If you want to propose this, the formal proposal process is at
On Thursday, 3 March 2016 11:42:26 UTC+11, tsuna wrote:

Hi all,
There seems to be at least 3 different ways to stub things out for
1. Create an interface, use one implementation for the real code,
another stubby one for testing.
2. Use a global variable to indirect the call, so it can be overridden
during tests, e.g.:
var timeNow = time.Now
3. Build different code using tags, e.g.:
// +build !test
func foo() { /* real implementation here */ }
// +build test
func foo() { /* fake implementation here */ }

Are there any other major ones that I’m missing that people find handy?

I’m starting to lean towards the 3rd above because it has zero runtime
cost, it cleanly separates the real implementation from the fake
implementation, and it doesn’t clutter the test code to override
global variables. But the big downside is that now running just “go
test ./...” doesn’t work anymore, people have to pass the extra “-tags

How many people would use the 3rd option if “go test” injected a
“test” tag in the build by default? Would the go team be open to this

This was discussed previously on the ML[1][2] but I’ve never seen any
“official” positioning on this topic from the go team. I’d be game to
send a code review with the change, but I wanted to first see how much
interest (or opposition) there would be.

[1] Just a couple recent threads on this topic:
“Proposal/question to make mocking easy”
“Stubbing functions for unit testing”

Benoit "tsuna" Sigoure
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
groupgolang-nuts @
postedMar 3, '16 at 12:42a
activeMar 6, '16 at 9:50a

2 users in discussion

Tsuna: 2 posts Dave Cheney: 1 post



site design / logo © 2023 Grokbase