Skip to content

Instantly share code, notes, and snippets.

@FrenchBen
Created August 9, 2018 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrenchBen/a8ed180ef7e4b844e898e340cfca9152 to your computer and use it in GitHub Desktop.
Save FrenchBen/a8ed180ef7e4b844e898e340cfca9152 to your computer and use it in GitHub Desktop.
Simple pFlag go test
package main
import (
"flag"
"fmt"
"github.com/spf13/pflag"
)
var (
argPort = pflag.Int("port", 8443, "The secure port to listen to for incoming HTTPS requests.")
)
func main() {
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
fmt.Printf("We got port: %d", *argPort)
}
package main
import (
"testing"
"github.com/spf13/pflag"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func TestCliFlags(t *testing.T) {
flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
err := flags.Parse([]string{
"--port=8080",
})
assert.Check(t, err)
assert.Check(t, is.Equal(8080, *argPort))
}
@FrenchBen
Copy link
Author

Output of test:

$ go test
--- FAIL: TestCliFlags (0.00s)
	main_test.go:16: assertion failed: error is not nil: unknown flag: --port
	main_test.go:17: assertion failed: 8080 (int) != 8443 (argPort int)
FAIL
exit status 1
FAIL	github.com/frenchben/cliex	0.008s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment