-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexec_test.go
135 lines (126 loc) · 3.72 KB
/
exec_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package testdeck
import (
"fmt"
"os/exec"
"strings"
"testing"
)
// Test against the real testing.T by executing in sub commands.
const examplePackage = "github.com/mercari/testdeck/demo"
const goBinary = "go"
var defaultArgs = []string{"test", "-timeout", "30s", "-v", examplePackage, "-run"}
// Execute a "go test" on an example testcase and return the output.
// We have to do this because the testing.T package doesn't export a lot of
// methods required to use it directly.
func execute(testName string) ([]byte, error) {
reTestName := fmt.Sprintf("^(%s)$", testName)
args := append(defaultArgs, reTestName)
return exec.Command(goBinary, args...).Output()
}
func Test_TestWithTestingT(t *testing.T) {
cases := map[string]struct {
testName string
wantStrings []string // strings that should be in the expected output
dontWantStrings []string // strings that should NOT be in the expected output
}{
"BasicExample_ActError": {
testName: "Test_BasicExample_ActError",
wantStrings: []string{
"--- FAIL: Test_BasicExample_ActError",
"basic act example error",
},
},
"BasicExample_ActFatal": {
testName: "Test_BasicExample_ActFatal",
wantStrings: []string{
"--- FAIL: Test_BasicExample_ActFatal",
"basic act example fatal",
},
},
"BasicExample_AssertErrorAndFatal": {
testName: "Test_BasicExample_AssertErrorAndFatal",
wantStrings: []string{
"--- FAIL: Test_BasicExample_AssertErrorAndFatal",
"basic assert example error",
"basic assert example fatal",
},
},
"BasicExample_ArrangeMultiFatal": {
testName: "Test_BasicExample_ArrangeMultiFatal",
wantStrings: []string{
"--- FAIL: Test_BasicExample_ArrangeMultiFatal",
"basic arrange example fatal 1",
},
dontWantStrings: []string{
"basic arrange example fatal 2",
},
},
"BasicExample_ArrangeAfterMultiFatal": {
testName: "Test_BasicExample_ArrangeAfterMultiFatal",
wantStrings: []string{
"--- FAIL: Test_BasicExample_ArrangeAfterMultiFatal",
"basic arrange example fatal 1",
"basic after example fatal 2",
},
},
"BasicExample_ArrangeFatalAndDeferred": {
testName: "Test_BasicExample_ArrangeFatalAndDeferred",
wantStrings: []string{
"--- FAIL: Test_BasicExample_ArrangeFatalAndDeferred",
"basic arrange example fatal 1",
"basic deferred output",
},
dontWantStrings: []string{
"basic arrange example fatal 2",
},
},
"ActEmptyNoError": {
testName: "Test_ActEmptyNoError",
wantStrings: []string{
"PASS: Test_ActEmptyNoError",
},
},
"ArrangeSkipNow_ShouldbeMarkedSkip": {
testName: "Test_ArrangeSkipNow_ShouldbeMarkedSkip",
wantStrings: []string{
"SKIP: Test_ArrangeSkipNow_ShouldbeMarkedSkip",
"skip now defer message 1",
},
dontWantStrings: []string{
"skip now defer message 2",
"skip now act error",
"skip now assert error",
"skip now after message",
},
},
"ActSkipNow_ShouldbeMarkedSkipAndExecuteAfter": {
testName: "Test_ActSkipNow_ShouldbeMarkedSkipAndExecuteAfter",
wantStrings: []string{
"SKIP: Test_ActSkipNow_ShouldbeMarkedSkipAndExecuteAfter",
"skip now after message",
"skip now defer message",
},
dontWantStrings: []string{
"skip now act error",
"skip now assert error",
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
out, _ := execute(tc.testName)
sout := string(out)
for _, want := range tc.wantStrings {
if !strings.Contains(sout, want) {
t.Errorf("Want output substring: %s\n", want)
}
}
for _, dontWant := range tc.dontWantStrings {
if strings.Contains(sout, dontWant) {
t.Errorf("Don't want output substring: %s\n", dontWant)
}
}
t.Logf("\nCommand Test Output:\n%s\n", sout)
})
}
}