CLI testing package for the Go language.
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.
The master branch might not be 100% stable. You should consider using one of the versions listed on https://github.com/rendon/testcli/releases.
main_test.go
// 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!")
}
}main.go
package main
import (
"fmt""os""github.com/codegangsta/cli"
)
funcmain() {
app:=cli.NewApp()
app.Name="cli"app.Usage="CLI app"app.Flags= []cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "User name",
},
}
app.Action=func(c*cli.Context) {
ifc.String("name") !="" {
fmt.Printf("Hello %s!\n", c.String("name"))
} else {
fmt.Printf("Hello? Anyone?\n")
}
}
app.Run(os.Args)
}