1

While writing a CLI tool that outputs to stdout, I noticed that if one test fails, then whatever the other (successful) tests had also written to stdout gets dumped out as well, which is misleading.

Is this to be expected, or should I set os.Stdout to /dev/null while testing? but then how would the testing package find anything to print out?

randomshinichi
  • 420
  • 5
  • 15
  • 3
    From the docs: `If a package test fails, go test prints the full test output`. This is the expected behavior. – JimB Feb 21 '19 at 15:06
  • That's not very helpful - if the test fails, go test prints the full output of the test and the successful tests that came before it, which isn't the expected behavior. I expect it to only print the full output of the test that failed. – randomshinichi Feb 21 '19 at 15:41
  • 1
    `stdout` isn't tied to specific tests, so the testing package has no way of filtering the stdout for only one test without making assumptions that may lose data which others want to see. Testing output should be done through the testing package, which writes to stderr. You can always run the failing test individually too. – JimB Feb 21 '19 at 16:18

2 Answers2

2

The test package doesn't interfere with the standard output of code under test, whether it passes or fails. If it's important for you not to see this output, you can capture stdout while executing your specific test and then decided what to do with it based on the test outcome.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
1

Try to use -failfast. Following an example.

$ go test -failfast -coverprofile=coverage.out -covermode=count <pkg path>
Camila Macedo
  • 857
  • 7
  • 10