I am newer to GO and currently working on a small CLI application. I am currently working on adding unit testing and have hit a snag and am wondering what the proper approach would be. I have a function that renders some output as a table to the command line using fmt.Println
/fmt.Printf
. I am wondering how I can capture that output in a unit test to make sure I am getting what is expected? Below is just a barebones skelton that somewhat represents what I am trying to achieve. Any tips would be a great help!
main.go
package main
import (
"fmt"
"io"
)
func print() {
fmt.Println("Hello world")
}
func main() {
print()
}
main_test.go
package main
import "testing"
func TestPrint(t *testing.T) {
expected := "Hello world"
print() // somehow capture the output
// if got != expected {
// t.Errorf("Does not match")
// }
}
I have tried a few approaches such as How to check a log/output in go test? but with minimal luck but, that could be due to misunderstanding on my end.
Aucun commentaire:
Enregistrer un commentaire