16

I'm trying to add an example to a package, and run the example via go test, however the example is never run.

For example, see this gist: https://gist.github.com/85469ecc65bb5bb85857

The gist has example_test.go:

package cow_test

import (
    cow "gist.github.com/85469ecc65bb5bb85857"
)

func Example() {
    cow.Poke()
}

Yet when I run this:

# go test -v example_test.go 
testing: warning: no tests to run
PASS
ok      command-line-arguments  0.002s

However other packages from stdlib work just fine:

# cd /usr/lib/go/src/errors
# go test -v example_test.go 
=== RUN: Example
--- PASS: Example (0.00s)
PASS
ok      command-line-arguments  0.002s

What is wrong with my example?

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
phemmer
  • 6,882
  • 3
  • 33
  • 31
  • 3
    You should [read the documentation about testing](http://golang.org/pkg/testing/) .. you have here some fundamentally wrong things that you should cover prior to attempting to write tests. – Simon Whitehead Mar 28 '15 at 04:58
  • 2
    ...I linked to the documentation for you... ? – Simon Whitehead Mar 28 '15 at 06:37
  • 11
    Your comment is basically a "RTFM" comment without any specific examples of what is wrong or where to focus. I see absolutely nothing in the scenario I have provided that goes against the testing documentation. Certainly nothing "fundamental". – phemmer Mar 28 '15 at 06:40
  • @SimonWhitehead Please see the accepted solution. The only thing wrong was missing a step. – phemmer Mar 28 '15 at 07:08
  • 2
    @Patrick RTFM can be a perfectly valid answer. Questioners are expected to do research and read the basic relevant documents and to not be [help vampires](http://slash7.com/2006/12/22/vampires/). – Dave C Mar 28 '15 at 14:45
  • 2
    @DaveC Except in this case I obviously had read the documentation, while Simon hadn't. If he had provided any additional info, this would have been apparent, and saved everyone headache. – phemmer Mar 28 '15 at 18:09
  • Opened a chat for further discussion on the topic: http://chat.stackoverflow.com/rooms/74004/29313626 . @SimonWhitehead, you are invited if you wish. – phemmer Mar 28 '15 at 19:53
  • @Patrick Re-reading this.. it appears I upset you and I definitely did not intend to. I will pick my words with more care next time .. and also take more time to interpret the question better. I originally read this as "I have this example, it doesn't work. Why not?". I have added a comment into your chat also. – Simon Whitehead Mar 29 '15 at 04:49

1 Answers1

28

From the documentation:

Example functions without output comments are compiled but not executed.

Add an output comment:

func Example() {
    junk.Poke()
    // Output: MOOOO!
}
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
  • 8
    Apparently it is also important **not** to have a space between the Output and the ":". Putting it here in case someone else accidentally adds space there and then wastes an hour figuring out why it doesn't run. – Buzzy May 22 '17 at 05:08
  • 1
    It's also important to ensure the `// Output: ` comment is the last comment. – freb Aug 02 '21 at 06:49
  • You may also put the actual output in a separate comment line after the line of `// Output:` – Eric Oct 24 '21 at 02:25
  • maybe for clarification: if the example does not output anything, a final `// Output:` without any output also sufficies. – TheDiveO Mar 12 '22 at 15:59
  • 1
    Heads up, does not work without the semicolon. – bart-kosmala Dec 28 '22 at 14:12