How to print from go test to console without using verbose option - go

I want to print an informative message to console from a test, but not have the verbose test output.
I have tried printing from the test using:
fmt.Println("my message") // STDOUT
fmt.Fprintln(os.Stderr, "my message") // STDERR
t.Log("my message\n") // testing.T log
which all produce the same effect of showing in the console if go test -v is used, but not showing if just go test is used.
However, with go test -v, I also get all the verbose test output like:
=== RUN My_Test
--- PASS: My_Test (0.07s)
go help testflag says:
-v
Verbose output: log all tests as they are run. Also print all
text from Log and Logf calls even if the test succeeds.
but what I want is to not log all tests as they are run, but still print all text from Log and Logf calls even if the test succeeds
Is there a way to print a visible message from within a test, but not see the RUN and PASS messages?

The testing framework "hijacks" the standard output and error streams for obvious reasons. So no matter what, whether writing to those streams appears in the output is controlled by the testing framework, and it provides no means to "customize" it other than showing or hiding all using the -v flag.
What you may do is use the -json testing flag:
-json
Log verbose output and test results in JSON. This presents the
same information as the -v flag in a machine-readable format.
So you get all the output you would otherwise get with -v, but you have a separate JSON object for each line.
Having this test function:
func TestMy_Test(t *testing.T) {
fmt.Println("[custom] my message from fmt.Println")
}
Output of go test -v .
=== RUN TestMy_Test
[custom] my message from fmt.Println
--- PASS: TestMy_Test (0.00s)
PASS
ok github.com/icza/play 0.002s
Output of go test -json .
{"Time":"2022-05-10T09:26:26.712800797+02:00","Action":"run","Package":"github.com/icza/play","Test":"TestMy_Test"}
{"Time":"2022-05-10T09:26:26.71293072+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"=== RUN TestMy_Test\n"}
{"Time":"2022-05-10T09:26:26.712946548+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}
{"Time":"2022-05-10T09:26:26.712954637+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"--- PASS: TestMy_Test (0.00s)\n"}
{"Time":"2022-05-10T09:26:26.712958774+02:00","Action":"pass","Package":"github.com/icza/play","Test":"TestMy_Test","Elapsed":0}
{"Time":"2022-05-10T09:26:26.712964812+02:00","Action":"output","Package":"github.com/icza/play","Output":"PASS\n"}
{"Time":"2022-05-10T09:26:26.713170439+02:00","Action":"output","Package":"github.com/icza/play","Output":"ok \tgithub.com/icza/play\t0.002s\n"}
{"Time":"2022-05-10T09:26:26.713573313+02:00","Action":"pass","Package":"github.com/icza/play","Elapsed":0.003}
You can write a simple app that processes and filters these JSON objects. Or you can filter the output as you could filter any other output.
Output of go test -json . |grep '\[custom\]'
{"Time":"2022-05-10T09:28:24.197077681+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}
If you also want the "pass" or "fail" messages, run go test -json . |grep '"pass"\|"fail"\|\[custom\]'
{"Time":"2022-05-10T09:29:26.069181336+02:00","Action":"output","Package":"github.com/icza/play","Test":"TestMy_Test","Output":"[custom] my message from fmt.Println\n"}
{"Time":"2022-05-10T09:29:26.069189228+02:00","Action":"pass","Package":"github.com/icza/play","Test":"TestMy_Test","Elapsed":0}
{"Time":"2022-05-10T09:29:26.069199239+02:00","Action":"pass","Package":"github.com/icza/play","Elapsed":0}

You can create you own Log function and use it for printing on screen
func Log(args ...interface{}) {
fmt.Fprintln(os.Stdout, args...)
}
You can also make it to print log based on condition passed through flag
var p = flag.Bool("p", false, "Enable Local Logging")
func MyLog(args ...interface{}) {
if *p {
fmt.Fprintln(os.Stdout, args...)
}
}
Example
package main
import (
"fmt"
"testing"
"os"
"flag"
)
var p = flag.Bool("p", false, "Enable Local Logging")
func Log(args ...interface{}) {
if *p {
fmt.Fprintln(os.Stdout, args...)
}
}
func IntMin(a, b int) int {
if a < b {
return a
}
return b
}
func TestIntMinBasic(t *testing.T) {
ans := IntMin(2, -2)
if ans != -2 {
t.Errorf("IntMin(2, -2) = %d; want -2", ans)
}
}
func TestIntMinTableDriven(t *testing.T) {
var tests = []struct {
a, b int
want int
}{
{0, 1, 0},
{1, 0, 0},
{2, -2, -2},
{0, -1, -1},
{-1, 0, -1},
}
Log("Print to Screen")
for _, tt := range tests {
testname := fmt.Sprintf("%d,%d", tt.a, tt.b)
t.Run(testname, func(t *testing.T) {
ans := IntMin(tt.a, tt.b)
if ans != tt.want {
t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
}
func BenchmarkIntMin(b *testing.B) {
for i := 0; i < b.N; i++ {
IntMin(1, 2)
}
}
And to pass the flag you can use -args
-args Pass the remainder of the command line (everything after -args) to the test binary, uninterpreted and unchanged. Because this flag consumes the remainder of the command line, the package list (if present) must appear before this flag.
Cmd Example:
go test -args -p

Although this doesn't print during the test, it prints straight after, which is better than not printing at all.
Define an os.File in your test file to write messages to:
var messagesFile *os.File
func messages() *os.File {
if messagesFile == nil {
messagesFile, _ = os.Create("tests.out")
}
return messagesFile
}
os.Create creates a new file if one doesn't exist, otherwise truncates the existing file.
Within your tests, write messages to that file:
messages().WriteString("my message")
Run your tests using go test, then cat the file. In my case, I use make:
test:
go test .
#cat tests.out
Output looks like:
ok <path to tests>
my message

Related

GoLand No Tests Were Run

I have a method that I am testing, and everything seems fine. However, when I run the tests in GoLand, I can see in the output that the tests "PASS" but the test runner says "no tests were run".
Here's the sample method in calculator.go
package calculator
import (
"fmt"
)
type Calculator struct {}
func New() Calculator {
return Calculator{}
}
func (s *Calculator) AddTwoNumbers(num_one, num_two int) int {
fmt.Printf("adding")
return num_one + num_two
}
Here's the test in calculator_test.go:
package calculator
import (
"fmt"
"testing"
)
func Test_Calculator_AddTwoNumbers(t *testing.T) {
// Arrange
calculator := New()
// Act
total := calculator.AddTwoNumbers(1,2)
// Assert
if total != 3 {
msg := fmt.Sprintf("total should have been %d but instead was %d", 3, total)
t.Error(msg)
}
}
What am I doing wrong?
Instead of fmt.Printf(... in AddTwoNumbers try either fmt.Println(... or fmt.Printf("foo\n')
The absence of the newline in the output of your AddTwoNumbers method is is causing the format of the test execution outputs to not have each test in a new line. The test runner is not being able to interpret that a test was run. Adding that newline, keeps a clean output.
I would suggest using to check the verbose output of the test run.
go test -v
-v
Verbose output: log all tests as they are run. Also print all
text from Log and Logf calls even if the test succeeds.

Having some issues recursively printing a complete list of a tree-like structure

I'm having trouble creating a structure for a CLI tool that will have n commands. Each command can have n subcommands, and each subcommand can have n more subcommands.
My issue is that, in Go, I'm struggling to figure out a way to create a recursive function to output the name of each command, along with each n subcommands + every n subcommands for that subcommand, in a complete list.
For example, I'm looking to get the following output:
1. command1
2. command2
3. command3
4. command3 subcommand1
5. command3 subcommand1 subcommand1
6. command3 subcommand2
Here is my code:
package main
import (
"fmt"
)
type command struct {
name string
parent *command
subcommands []*command
}
func getLastCommand(c command) command {
for _, s := range c.subcommands {
if len(s.subcommands) == 0 {
return *s
}
return getLastCommand(*s)
}
return c
}
func main() {
cmdBase1 := command{
name: "base1",
}
cmdBase2 := command{
name: "base2",
}
var (
cmdBase3,
cmdBase3Sub1,
cmdBase3Sub1Sub1,
cmdBase3Sub2 command
)
cmdBase3 = command{
name: "base3",
subcommands: []*command{&cmdBase3Sub1, &cmdBase3Sub2},
}
cmdBase3Sub1 = command{
name: "base3:sub1",
parent: &cmdBase3,
subcommands: []*command{&cmdBase3Sub1Sub1},
}
cmdBase3Sub1Sub1 = command{
name: "base3:sub1:sub1",
parent: &cmdBase3Sub1,
}
cmdBase3Sub2 = command{
name: "base3:sub2",
parent: &cmdBase3,
}
// root commands
commands := []command{
cmdBase1,
cmdBase2,
cmdBase3,
}
for _, c := range commands {
last := getLastCommand(c)
fmt.Println(last.name)
}
}
https://play.golang.org/p/HZPRlSghfAY
Here is the current output:
base1
base2
base3:sub1:sub1
My desired output is with the above code is:
base1
base2
base3
base3:sub1
base3:sub1:sub1
base3:sub2
What do I need to change in my code so I can get my above desired output? Is there an algorithm or data structure that I could follow to solve this? I've tried depth-first and binary searching, but I can't seem to mold it to my structure.
A simple and elegant solution would be to "arm" command with a print() method. This could print its name, and range over its subcommands, calling their print() (which do the same):
func (c *command) print() {
fmt.Println(c.name)
for _, sc := range c.subcommands {
sc.print()
}
}
Then printing the commands in main() is just calling their print() method (getLastCommand() is not even needed / used):
for _, c := range commands {
c.print()
}
This will result in your desired output (try it on the Go Playground):
base1
base2
base3
base3:sub1
base3:sub1:sub1
base3:sub2
Note that of course print() doesn't have to be a method, it may be a regular function too, in which case it could look like this:
func print(c *command) {
fmt.Println(c.name)
for _, sc := range c.subcommands {
print(sc)
}
}
And the loop in main():
for _, c := range commands {
print(&c)
}
Result is the same, try this one on the Go Playground.
I would also suggest to be consistent. If you decide to use pointer to command, do it everywhere (e.g. the commands slice in your main() stores non-pointers, that's why its element's address had to be taken to pass to print()).

GO: Run cli command with wrong args

I use cobra to create CLI command tool.
everything is looking OK except the error handling
what I want that if command was sent by mistake (wrong args or wrong input) return std.err instead of std.out
to simplify the secnario I've created this which demonstrate my use-case
package main
import (
"errors"
"fmt"
"os"
"github.com/spf13/cobra"
)
var (
RootCmd = &cobra.Command{
Use: "myApp",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("ROOT verbose = %d, args = %v\n", args)
},
}
provideCmd = &cobra.Command{
Use: "provide",
Run: nil,
}
appCmd = &cobra.Command{
Use: "apps",
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
if name != "myapp" {
err := errors.New("app name doesnt exist")
return err
}
return nil
},
SilenceUsage: true,
}
)
func init() {
// Add the application command to app command
provideCmd.AddCommand(appCmd)
// add provide command to root command
RootCmd.AddCommand(provideCmd)
}
func main() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
Now if I compile the binary and run exec.Command against the binary everything is working as expected. but if I want to test the error scenario like mycli provide apps apps1
I want to see that returned in std.err and not at std.out
When I execute mycli provide apps myapp everything should be OK
but if I run mycli provide apps myapp2 I want to get std.err and not std.out , which is not the case here ...what am I missing here ?
https://play.golang.org/p/B00z4eZ7Sj-
Your sample already prints the error both to stdout and stderr.
By default the cobra package prints any errors it encounters to stderr, unless you specifically change that.
So running
./main provide apps something 2> ./stderr.txt creates a text file with the following content (this is what cobra writes to stderr without your intervention):
Error: app name doesnt exist
And running ./main provide apps something > ./stdout.txt - creates a text file with the following content (you printed that yourself with fmt.Println(err), the second line from the bottom in your code):
app name doesnt exist
Which means default behaviour prints errors both to stdout and stderr.
As Devin has advised you, changing the last line to os.Stderr.WriteString(err) or
fmt.Fprintln(os.Stderr, err) (the one I would use) will make your project to print everything to stderr only, which means printing errors twice:
Error: app name doesnt exist
app name doesnt exist
It might be useful to know that cobra allows you some control of error printing behaviour. For example, you can tell a cobra command which stream to print to:
command.SetOutput(os.Stdout) // Defaults to os.Stderr
you could also prevent printing of errors:
command.SilenceErrors = true
or prevent printing of usage text:
command.SilenceUsage = true

Is there any command line in Golang to check syntax error of my go source code & write the errors to a file?

package main
import "fmt"
func plus(a int, b int) int {
return a + b
}
}
func plusPlus(a, b, c int) int {
return a + b + c
}
func main() {
res := plus(1, 2)
fmt.Println("1+2 =", res)
res = plusPlus(1, 2, 3)
fmt.Println("1+2+3 =", res)
}
This is my Go Source code.
gofmt -e my_file.go is working But I am not able to write the errors to a text file.
gofmt will output the errors to the standard error. You can simply redirect the standard error stream of gofmt to a file on unix systems like this:
gofmt -e my_file.go 2>a.txt
Checking it:
more a.txt
Output:
my_file.go:8:1: expected declaration, found '}'

Compare Go stdout to file contents with testing package [duplicate]

I have a simple function I want to test:
func (t *Thing) print(min_verbosity int, message string) {
if t.verbosity >= minv {
fmt.Print(message)
}
}
But how can I test what the function actually sends to standard output? Test::Output does what I want in Perl. I know I could write all my own boilerplate to do the same in Go (as described here):
orig = os.Stdout
r,w,_ = os.Pipe()
thing.print("Some message")
var buf bytes.Buffer
io.Copy(&buf, r)
w.Close()
os.Stdout = orig
if(buf.String() != "Some message") {
t.Error("Failure!")
}
But that's a lot of extra work for every single test. I'm hoping there's a more standard way, or perhaps an abstraction library to handle this.
One thing to also remember, there's nothing stopping you from writing functions to avoid the boilerplate.
For example I have a command line app that uses log and I wrote this function:
func captureOutput(f func()) string {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return buf.String()
}
Then used it like this:
output := captureOutput(func() {
client.RemoveCertificate("www.example.com")
})
assert.Equal(t, "removed certificate www.example.com\n", output)
Using this assert library: http://godoc.org/github.com/stretchr/testify/assert.
You can do one of three things. The first is to use Examples.
The package also runs and verifies example code. Example functions may include a concluding line comment that begins with "Output:" and is compared with the standard output of the function when the tests are run. (The comparison ignores leading and trailing space.) These are examples of an example:
func ExampleHello() {
fmt.Println("hello")
// Output: hello
}
The second (and more appropriate, IMO) is to use fake functions for your IO. In your code you do:
var myPrint = fmt.Print
func (t *Thing) print(min_verbosity int, message string) {
if t.verbosity >= minv {
myPrint(message) // N.B.
}
}
And in your tests:
func init() {
myPrint = fakePrint // fakePrint records everything it's supposed to print.
}
func Test...
The third is to use fmt.Fprintf with an io.Writer that is os.Stdout in production code, but bytes.Buffer in tests.
You could consider adding a return statement to your function to return the string that is actually printed out.
func (t *Thing) print(min_verbosity int, message string) string {
if t.verbosity >= minv {
fmt.Print(message)
return message
}
return ""
}
Now, your test could just check the returned string against an expected string (rather than the print out). Maybe a bit more in-line with Test Driven Development (TDD).
And, in your production code, nothing would need to change, since you don't have to assign the return value of a function if you don't need it.

Resources