I want to test different (correct/incorrect) command line arguments passed to my CLI program, but I am not sure how to achieve this with go/testing package because I am getting flag redefined error. Looks like it happens because flag.Parse() can be called only once. What is the proper approach to test different command line arguments passed into the go program? Is there is any way to define something like setup()/teardown() or run every case in isolation (but in the same file)?
Here is my code:
Function to test:
func (p *Params) Parse() (*Params, error) {
param1Ptr := flag.String("param1", "default", "param1 desc")
param2Ptr := flag.String("param2", "default", "param1 desc")
...
...
flag.Parse()
...
}
Test file:
package main
import (
"os"
"testing"
)
func TestParam1(t *testing.T) {
os.Args = []string{"cmd", "-param1", "incorrect", "-param2", "correct"}
params := Params{}
_, err := params.Parse()
...
...
}
func TestParam2(t *testing.T) {
os.Args = []string{"cmd", "-param1", "correct", "-param2", "incorrect"}
params := Params{}
_, err := params.Parse()
...
...
}