此代码正在执行二进制命令,并返回std.out和std.error。
func exe(bin string, args string, path string) (string, error string) {
cmd := exec.Command(bin, strings.Split(args, " ")...)
cmd.Dir = path
stdoutBuf := &bytes.Buffer{}
cmd.Stdout = stdoutBuf
stdErrBuf := &bytes.Buffer{}
cmd.Stderr = stdErrBuf
cmd.Run()
return stdoutBuf.String(), stdErrBuf.String()
}我不知道如何为它运行一个好的测试的问题,它将在每个系统中得到支持,例如,如果我试图运行"echo“命令,测试运行在达尔文和Linux上,而不是在windows上。我该怎么做?
func Test_execute(t *testing.T) {
type args struct {
bin string
args string
path string
}
tests := []struct {
name string
args args
wantString string
wantError string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotString, gotError := exe(tt.args.bin, tt.args.args, tt.args.path)
if gotString != tt.wantString {
t.Errorf("exe() gotString = %v, want %v", gotString, tt.wantString)
}
if gotError != tt.wantError {
t.Errorf("exe() gotError = %v, want %v", gotError, tt.wantError)
}
})
}
}我搜索了它并找到了这个,https://www.joeshaw.org/testing-with-os-exec-and-testmain/,但是现在确定如何将env与我的测试相结合.
发布于 2018-11-21 21:26:02
使用Go生成标记或文件名。例如,对于Linux和Windows:
a_linux_test.go (Linux文件名):
package main
import "testing"
func TestLinuxA(t *testing.T) {
t.Log("Linux A")
}l_test.go (Linux构建标记):
// +build linux
package main
import "testing"
func TestLinuxL(t *testing.T) {
t.Log("Linux L")
}a_windows_test.go (Windows文件名):
package main
import "testing"
func TestWindowsA(t *testing.T) {
t.Log("Windows A")
}w_test.go (Windows标记):
// +build windows
package main
import "testing"
func TestWindowsW(t *testing.T) {
t.Log("Windows W")
}输出(在Linux上):
$ go test -v
=== RUN TestLinuxA
--- PASS: TestLinuxA (0.00s)
a_linux_test.go:6: Linux A
=== RUN TestLinuxL
--- PASS: TestLinuxL (0.00s)
l_test.go:8: Linux L
PASS
$ 输出(在Windows上):
>go test -v
=== RUN TestWindowsA
--- PASS: TestWindowsA (0.00s)
a_windows_test.go:6: Windows A
=== RUN TestWindowsW
--- PASS: TestWindowsW (0.00s)
w_test.go:8: Windows W
PASS
>参考文献:
https://stackoverflow.com/questions/53420303
复制相似问题