如何在go中测试系统命令

时间:2014-06-18 13:20:19

标签: go

鉴于我的功能如下:

func getTotalMemory() string {
  out,_ := exec.Command("grep", "MemTotal", "/proc/meminfo").Output()
  t := strings.Split(string(out), ":")
  x := strings.TrimSpace(t[1])
  return x
}

如何为该功能编写测试以确保我正确解析它?在红宝石中,我会做类似

的事情
os.expects(:Command).and_returns("string")

我目前正在使用GoConvey,如果这对答案有任何影响。

谢谢!

1 个答案:

答案 0 :(得分:4)

您可以先查看os.exec package的测试政策:
见" src/pkg/os/exec/exec_test.go"。

注意:GoConvey与标准Go测试框架兼容,因此不会影响您想要进行的测试。