The Go test tutorials I have searched use the return value to the testing.
I would like to write a test that checks the output from the terminal.
Here is an example of what I want to achieve:
main.go
package main
import (
"fmt"
"os"
"strings"
)
func main() {
arguments := os.Args
fmt.Println(strings.ToUpper(arguments[1]))
}
The test I have played around with
main_test.go
package main
import (
"log"
"os/exec"
"testing"
)
type checkResult struct {
inp string
expected string
}
var testCases = []checkResult{
{"Go <3", "GO <3"},
}
func TestMain(t *testing.T) {
for _, test := range testCases {
output, err := exec.Command("go run main.go ", test.inp).Output()
if err != nil {
log.Fatal(err)
}
if string(output) != (test.expected) {
t.Fatal("Expected result is wrong")
}
}
}