Is it possible to emulate Example testing with Gomega?

421 views Asked by At

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?

1

There are 1 answers

2
Onsi Fakhouri On

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?