Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you print in a Go test using the "testing" package?

Tags:

testing

go

I'm running a test in Go with a statement to print something (i.e. for debugging of tests) but it's not printing anything.

func TestPrintSomething(t *testing.T) {
    fmt.Println("Say hi")
}

When I run go test on this file, this is the output:

ok      command-line-arguments  0.004s

The only way to really get it to print, as far as I know, is to print it via t.Error(), like so:

func TestPrintSomethingAgain(t *testing.T) {
    t.Error("Say hi")
}

Which outputs this:

Say hi
--- FAIL: TestPrintSomethingAgain (0.00 seconds)
    foo_test.go:35: Say hi
FAIL
FAIL    command-line-arguments  0.003s
gom:  exit status 1

I've Googled and looked through the manual but didn't find anything.

like image 220
platwp Avatar asked Oct 13 '22 22:10

platwp


People also ask

How do you test a go package?

Once you write the test, you can run it using the go test command. The go test command will look at all Go files in the current directory where you're running the command form. go test will look for two keywords: Any Go file that ends with _test.go. All function names that start with Test.

How do you run all tests in go?

To execute all the test in the current package, use command go test . Go will run all the test files along with other files except main function. If there testing.

How do I write a test in go?

In this step, you will write your first test in Go. Writing tests in Go requires a test file link, and this test file must always end with _test.go. By convention, Go testing files are always located in the same folder, or package, where the code they are testing resides.

How to write a test suite in Golang?

Steps for writing test suite in Golang: 1 Create a file whose name ends with _test.go. 2 Import package testing by import “testing” command. 3 Write the test function of form func TestXxx (*testing.T) which uses any of Error, Fail, or related methods to signal failure. 4 Put the file in any package. 5 Run command go test.

What do I need to complete the go unit testing tutorial?

Once you complete the tutorial, you will have a working unit-testing suite that includes a table-based unit test, a coverage test, a benchmark, and a documented example. To complete this tutorial, you’ll need the following: A familiarity with the Go programming language.

What is the difference between automated testing and package testing?

Because automated testing is done using an automation tool, exploration tests take less time and more test scripts, while increasing the overall scope of the tests. In Golang, package testing is responsible for different types of testing maybe it is performance testing, parallel testing, functional testing, or any possible combination of these all.


2 Answers

The structs testing.T and testing.B both have a .Log and .Logf method that sound to be what you are looking for. .Log and .Logf are similar to fmt.Print and fmt.Printf respectively.

See more details here: http://golang.org/pkg/testing/#pkg-index

fmt.X print statements do work inside tests, but you will find their output is probably not on screen where you expect to find it and, hence, why you should use the logging methods in testing.

If, as in your case, you want to see the logs for tests that are not failing, you have to provide go test the -v flag (v for verbosity). More details on testing flags can be found here: https://golang.org/cmd/go/#hdr-Testing_flags

like image 243
voidlogic Avatar answered Oct 16 '22 11:10

voidlogic


For example,

package verbose

import (
    "fmt"
    "testing"
)

func TestPrintSomething(t *testing.T) {
    fmt.Println("Say hi")
    t.Log("Say bye")
}

go test -v
=== RUN TestPrintSomething
Say hi
--- PASS: TestPrintSomething (0.00 seconds)
    v_test.go:10: Say bye
PASS
ok      so/v    0.002s

Command go

Description of testing flags

-v
Verbose output: log all tests as they are run. Also print all
text from Log and Logf calls even if the test succeeds.

Package testing

func (*T) Log

func (c *T) Log(args ...interface{})

Log formats its arguments using default formatting, analogous to Println, and records the text in the error log. For tests, the text will be printed only if the test fails or the -test.v flag is set. For benchmarks, the text is always printed to avoid having performance depend on the value of the -test.v flag.

like image 203
peterSO Avatar answered Oct 16 '22 10:10

peterSO