How to test for execution process
This code is executing commands for binary and return std.out
and 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()
}
The problem that I don't know how to run a good test for it which will be supported in each system
e.g. if I try to run "echo" command the test run on Darwin and Linux but not on windows. how I should do it?
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)
}
})
}
}
I've searched on it and found this,
https://www.joeshaw.org/testing-with-os-exec-and-testmain/
But now sure how to combine the env with my test...
go
add a comment |
This code is executing commands for binary and return std.out
and 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()
}
The problem that I don't know how to run a good test for it which will be supported in each system
e.g. if I try to run "echo" command the test run on Darwin and Linux but not on windows. how I should do it?
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)
}
})
}
}
I've searched on it and found this,
https://www.joeshaw.org/testing-with-os-exec-and-testmain/
But now sure how to combine the env with my test...
go
Run different commands on different platforms
– zerkms
Nov 21 '18 at 20:55
@zerkms - can you please provide example on which commands?
– user6124024
Nov 21 '18 at 20:56
seeing as we don't know if you are on plan9 or windows 98 the guesswork is yours :)
– Vorsprung
Nov 21 '18 at 21:15
@Vorsprung - well,I mean just for win10/darwin latest and linux latest
– user6124024
Nov 21 '18 at 21:18
@Vorsprung - but any example which can run on win10 and linux will be sufficient for me
– user6124024
Nov 21 '18 at 21:23
add a comment |
This code is executing commands for binary and return std.out
and 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()
}
The problem that I don't know how to run a good test for it which will be supported in each system
e.g. if I try to run "echo" command the test run on Darwin and Linux but not on windows. how I should do it?
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)
}
})
}
}
I've searched on it and found this,
https://www.joeshaw.org/testing-with-os-exec-and-testmain/
But now sure how to combine the env with my test...
go
This code is executing commands for binary and return std.out
and 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()
}
The problem that I don't know how to run a good test for it which will be supported in each system
e.g. if I try to run "echo" command the test run on Darwin and Linux but not on windows. how I should do it?
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)
}
})
}
}
I've searched on it and found this,
https://www.joeshaw.org/testing-with-os-exec-and-testmain/
But now sure how to combine the env with my test...
go
go
edited Nov 22 '18 at 7:49
Flimzy
40.1k1366100
40.1k1366100
asked Nov 21 '18 at 20:52
user6124024
Run different commands on different platforms
– zerkms
Nov 21 '18 at 20:55
@zerkms - can you please provide example on which commands?
– user6124024
Nov 21 '18 at 20:56
seeing as we don't know if you are on plan9 or windows 98 the guesswork is yours :)
– Vorsprung
Nov 21 '18 at 21:15
@Vorsprung - well,I mean just for win10/darwin latest and linux latest
– user6124024
Nov 21 '18 at 21:18
@Vorsprung - but any example which can run on win10 and linux will be sufficient for me
– user6124024
Nov 21 '18 at 21:23
add a comment |
Run different commands on different platforms
– zerkms
Nov 21 '18 at 20:55
@zerkms - can you please provide example on which commands?
– user6124024
Nov 21 '18 at 20:56
seeing as we don't know if you are on plan9 or windows 98 the guesswork is yours :)
– Vorsprung
Nov 21 '18 at 21:15
@Vorsprung - well,I mean just for win10/darwin latest and linux latest
– user6124024
Nov 21 '18 at 21:18
@Vorsprung - but any example which can run on win10 and linux will be sufficient for me
– user6124024
Nov 21 '18 at 21:23
Run different commands on different platforms
– zerkms
Nov 21 '18 at 20:55
Run different commands on different platforms
– zerkms
Nov 21 '18 at 20:55
@zerkms - can you please provide example on which commands?
– user6124024
Nov 21 '18 at 20:56
@zerkms - can you please provide example on which commands?
– user6124024
Nov 21 '18 at 20:56
seeing as we don't know if you are on plan9 or windows 98 the guesswork is yours :)
– Vorsprung
Nov 21 '18 at 21:15
seeing as we don't know if you are on plan9 or windows 98 the guesswork is yours :)
– Vorsprung
Nov 21 '18 at 21:15
@Vorsprung - well,I mean just for win10/darwin latest and linux latest
– user6124024
Nov 21 '18 at 21:18
@Vorsprung - well,I mean just for win10/darwin latest and linux latest
– user6124024
Nov 21 '18 at 21:18
@Vorsprung - but any example which can run on win10 and linux will be sufficient for me
– user6124024
Nov 21 '18 at 21:23
@Vorsprung - but any example which can run on win10 and linux will be sufficient for me
– user6124024
Nov 21 '18 at 21:23
add a comment |
1 Answer
1
active
oldest
votes
Use Go build tags or file names. For example, for Linux and Windows:
a_linux_test.go
(Linux file name):
package main
import "testing"
func TestLinuxA(t *testing.T) {
t.Log("Linux A")
}
l_test.go
(Linux build tag):
// +build linux
package main
import "testing"
func TestLinuxL(t *testing.T) {
t.Log("Linux L")
}
a_windows_test.go
(Windows file name):
package main
import "testing"
func TestWindowsA(t *testing.T) {
t.Log("Windows A")
}
w_test.go
(Windows build tag):
// +build windows
package main
import "testing"
func TestWindowsW(t *testing.T) {
t.Log("Windows W")
}
Output (on 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
$
Output (on 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
>
References:
Package build
Package testing
Command go
Thanks 1+ , It will be great if you can add to the example or linux or windows (which is more simple to you) the complite test that i put , this will help to understand this trick better . thanks!
– user6124024
Nov 21 '18 at 21:46
I mean put the test inside your code and which is command to call ...this is what I missing here ...
– user6124024
Nov 21 '18 at 21:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420303%2fhow-to-test-for-execution-process%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use Go build tags or file names. For example, for Linux and Windows:
a_linux_test.go
(Linux file name):
package main
import "testing"
func TestLinuxA(t *testing.T) {
t.Log("Linux A")
}
l_test.go
(Linux build tag):
// +build linux
package main
import "testing"
func TestLinuxL(t *testing.T) {
t.Log("Linux L")
}
a_windows_test.go
(Windows file name):
package main
import "testing"
func TestWindowsA(t *testing.T) {
t.Log("Windows A")
}
w_test.go
(Windows build tag):
// +build windows
package main
import "testing"
func TestWindowsW(t *testing.T) {
t.Log("Windows W")
}
Output (on 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
$
Output (on 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
>
References:
Package build
Package testing
Command go
Thanks 1+ , It will be great if you can add to the example or linux or windows (which is more simple to you) the complite test that i put , this will help to understand this trick better . thanks!
– user6124024
Nov 21 '18 at 21:46
I mean put the test inside your code and which is command to call ...this is what I missing here ...
– user6124024
Nov 21 '18 at 21:47
add a comment |
Use Go build tags or file names. For example, for Linux and Windows:
a_linux_test.go
(Linux file name):
package main
import "testing"
func TestLinuxA(t *testing.T) {
t.Log("Linux A")
}
l_test.go
(Linux build tag):
// +build linux
package main
import "testing"
func TestLinuxL(t *testing.T) {
t.Log("Linux L")
}
a_windows_test.go
(Windows file name):
package main
import "testing"
func TestWindowsA(t *testing.T) {
t.Log("Windows A")
}
w_test.go
(Windows build tag):
// +build windows
package main
import "testing"
func TestWindowsW(t *testing.T) {
t.Log("Windows W")
}
Output (on 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
$
Output (on 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
>
References:
Package build
Package testing
Command go
Thanks 1+ , It will be great if you can add to the example or linux or windows (which is more simple to you) the complite test that i put , this will help to understand this trick better . thanks!
– user6124024
Nov 21 '18 at 21:46
I mean put the test inside your code and which is command to call ...this is what I missing here ...
– user6124024
Nov 21 '18 at 21:47
add a comment |
Use Go build tags or file names. For example, for Linux and Windows:
a_linux_test.go
(Linux file name):
package main
import "testing"
func TestLinuxA(t *testing.T) {
t.Log("Linux A")
}
l_test.go
(Linux build tag):
// +build linux
package main
import "testing"
func TestLinuxL(t *testing.T) {
t.Log("Linux L")
}
a_windows_test.go
(Windows file name):
package main
import "testing"
func TestWindowsA(t *testing.T) {
t.Log("Windows A")
}
w_test.go
(Windows build tag):
// +build windows
package main
import "testing"
func TestWindowsW(t *testing.T) {
t.Log("Windows W")
}
Output (on 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
$
Output (on 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
>
References:
Package build
Package testing
Command go
Use Go build tags or file names. For example, for Linux and Windows:
a_linux_test.go
(Linux file name):
package main
import "testing"
func TestLinuxA(t *testing.T) {
t.Log("Linux A")
}
l_test.go
(Linux build tag):
// +build linux
package main
import "testing"
func TestLinuxL(t *testing.T) {
t.Log("Linux L")
}
a_windows_test.go
(Windows file name):
package main
import "testing"
func TestWindowsA(t *testing.T) {
t.Log("Windows A")
}
w_test.go
(Windows build tag):
// +build windows
package main
import "testing"
func TestWindowsW(t *testing.T) {
t.Log("Windows W")
}
Output (on 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
$
Output (on 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
>
References:
Package build
Package testing
Command go
edited Nov 21 '18 at 21:40
answered Nov 21 '18 at 21:26
peterSOpeterSO
98.2k15165182
98.2k15165182
Thanks 1+ , It will be great if you can add to the example or linux or windows (which is more simple to you) the complite test that i put , this will help to understand this trick better . thanks!
– user6124024
Nov 21 '18 at 21:46
I mean put the test inside your code and which is command to call ...this is what I missing here ...
– user6124024
Nov 21 '18 at 21:47
add a comment |
Thanks 1+ , It will be great if you can add to the example or linux or windows (which is more simple to you) the complite test that i put , this will help to understand this trick better . thanks!
– user6124024
Nov 21 '18 at 21:46
I mean put the test inside your code and which is command to call ...this is what I missing here ...
– user6124024
Nov 21 '18 at 21:47
Thanks 1+ , It will be great if you can add to the example or linux or windows (which is more simple to you) the complite test that i put , this will help to understand this trick better . thanks!
– user6124024
Nov 21 '18 at 21:46
Thanks 1+ , It will be great if you can add to the example or linux or windows (which is more simple to you) the complite test that i put , this will help to understand this trick better . thanks!
– user6124024
Nov 21 '18 at 21:46
I mean put the test inside your code and which is command to call ...this is what I missing here ...
– user6124024
Nov 21 '18 at 21:47
I mean put the test inside your code and which is command to call ...this is what I missing here ...
– user6124024
Nov 21 '18 at 21:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420303%2fhow-to-test-for-execution-process%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Run different commands on different platforms
– zerkms
Nov 21 '18 at 20:55
@zerkms - can you please provide example on which commands?
– user6124024
Nov 21 '18 at 20:56
seeing as we don't know if you are on plan9 or windows 98 the guesswork is yours :)
– Vorsprung
Nov 21 '18 at 21:15
@Vorsprung - well,I mean just for win10/darwin latest and linux latest
– user6124024
Nov 21 '18 at 21:18
@Vorsprung - but any example which can run on win10 and linux will be sufficient for me
– user6124024
Nov 21 '18 at 21:23