You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Developing a command line application? Wanna be able to test your app from the outside? If the answer is Yes to at least one of the questions, keep reading.
When using Ruby I use aruba for testing command line applications, in Go I still can use aruba, but it"s awkward to bring Ruby and it's artillery only to test my app.
testcli is a wrapper around os.exec to test CLI apps in Go lang, minimalistic, so you can do your tests with testing or any other testing framework.
// make sure to execute `go install` before testspackage main
import (
"testing""github.com/rendon/testcli"
)
funcTestGreetings(t*testing.T) {
// Using package functionstestcli.Run("greetings")
if!testcli.Success() {
t.Fatalf("Expected to succeed, but failed: %s", testcli.Error())
}
if!testcli.StdoutContains("Hello?") {
t.Fatalf("Expected %q to contain %q", testcli.Stdout(), "Hello?")
}
}
funcTestGreetingsWithName(t*testing.T) {
// Using the struct version, if you want to test multiple commandsc:=testcli.Command("greetings", "--name", "John")
c.Run()
if!c.Success() {
t.Fatalf("Expected to succeed, but failed with error: %s", c.Error())
}
if!c.StdoutContains("Hello John!") {
t.Fatalf("Expected %q to contain %q", c.Stdout(), "Hello John!")
}
}