How to mock exec.Command for multiple unit tests in Go lang? -


i learnt unit testing functions uses exec.command() i.e., mocking exec.command(). went ahead added more unit cases, running issues of not able mock output different scenarios.

here sample code hello.go i'm trying test...

package main  import (     "fmt"     "os/exec" )  var execcommand = exec.command  func printdate() ([]byte, error) {     cmd := execcommand("date")     out, err := cmd.combinedoutput()     return out, err }  func main() {     fmt.printf("hello, world\n")     fmt.println(printdate()) } 

below test code hello_test.go...

package main  import (     "fmt"     "os"     "os/exec"     "testing" )  var mockedexitstatus = 1 var mockeddate = "sun aug 20" var expdate = "sun aug 20"  func fakeexeccommand(command string, args ...string) *exec.cmd {     cs := []string{"-test.run=testhelperprocess", "--", command}     cs = append(cs, args...)     cmd := exec.command(os.args[0], cs...)     cmd.env = []string{"go_want_helper_process=1"}     return cmd }  func testhelperprocess(t *testing.t) {     if os.getenv("go_want_helper_process") != "1" {         return     }      // println("mocked data:", mockeddate)     fmt.fprintf(os.stdout, mockeddate)     os.exit(mockedexitstatus) }  func testprintdate(t *testing.t) {     execcommand = fakeexeccommand     defer func() { execcommand = exec.command }()      out, err := printdate()     print("std out: ", string(out))     if err != nil {         t.errorf("expected nil error, got %#v", err)     }     if string(out) != expdate {         t.errorf("expected %q, got %q", expdate, string(out))     } }  func testprintdateunabletorunerror(t *testing.t) {     execcommand = fakeexeccommand     defer func() { execcommand = exec.command }()      mockedexitstatus = 1     mockeddate = "unable run date command"     expdate = "unable run date command"      out, err := printdate()     print("std out: ", string(out))     if err != nil {         t.errorf("expected nil error, got %#v", err)     }     if string(out) != expdate {         t.errorf("expected %q, got %q", expdate, string(out))     } } 

go test fails second test testprintdateunabletorunerror...

$ go test hello std out: sun aug 20std out: sun aug 20--- fail: testprintdatetomorrow (0.01s)     hello_test.go:62: expected "unable run date command", got "sun aug 20" fail fail    hello   0.017s 

even though i'm trying set global mockeddate value inside test case, it's still getting global value initialized with. is global value not getting set? or changes global var not getting updated in testhelperprocess?

based on code you've posted, mockeddate variable doesn't anything. neither test, nor call printdate() utilizing it, testprintdateunabletorunerror() test performs tests before it.

if add functionality printdate() function return string of "unable run date command" (when case), condition on line 62 pass. said, such checks should unnecessary, when have error in return values printdate(). if returned error non-nil, returned output string should expected invalid (or empty, "").

i can't tell how want printdate() fail, stands, there's no way return values you're expecting in testprintdateunabletorunerror().


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -