0

I have some go tests that I would like to update to use Ginkgo for BDD style testing. The problem is, the server uses stdout and stderr for logging, and many of the tests utilize Go's built in "Example" testing framework as follows:

import (
    "fmt"
)

func ExampleConsoleLog() {
    fmt.Printf("Testing %d, %d, %d: %s\n", 1, 2, 3, "mike check")

    // Output:
    // Testing 1, 2, 3: mike check
}

I would like to using Ginkgo and Gomega to assert that these get printed out to the stdout, but there are no built in matchers that I can tell that do this. Gomega does provide a gbytes package, but there is no documentation on how to "attach" a gbytes.Buffer to stdout or stderr. Is there is a custom matcher that I can use for this?

bbengfort
  • 5,254
  • 4
  • 44
  • 57

1 Answers1

0

You can compile a binary and launch it & test its output via gexec and gbytes: http://onsi.github.io/gomega/#gexec-testing-external-processes

If you're trying to test the output of a particular function (without compiling and launching an external library) the cleanest way will likely be to inject an io.Writer and then pass in your gbytes buffer. There are other options, though they're somewhat inelegant: In Go, how do I capture stdout of a function into a string?

Community
  • 1
  • 1
  • Also: https://stackoverflow.com/questions/25609734/testing-stdout-with-go-and-ginkgo – Onsi Fakhouri Jan 03 '15 at 15:35
  • This is a command line library that I'm trying to test, so the gexec methodology won't work for me. As for dependency injection, part of what this library does is ensure that it has access to stdout and stderr so they're not redirected by a logging mechanism (due to the daemonization of the rest of the app). This is why the Example test methodology works so well. I guess I have to capture stdout and sterr, but that's less elegant, so I might just go back to regular go test. – bbengfort Jan 12 '15 at 18:44