-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvenom_output.go
142 lines (127 loc) · 3.23 KB
/
venom_output.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
136
137
138
139
140
141
142
package venom
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"time"
"github.com/fatih/color"
tap "github.com/mndrix/tap-go"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
// OutputResult output result to sdtout, files...
func (v *Venom) OutputResult(tests Tests, elapsed time.Duration) error {
var data []byte
var err error
switch v.OutputFormat {
case "json":
data, err = json.MarshalIndent(tests, "", " ")
if err != nil {
log.Fatalf("Error: cannot format output json (%s)", err)
}
case "tap":
data, err = outputTapFormat(tests)
if err != nil {
log.Fatalf("Error: cannot format output tap (%s)", err)
}
case "yml", "yaml":
data, err = yaml.Marshal(tests)
if err != nil {
log.Fatalf("Error: cannot format output yaml (%s)", err)
}
default:
dataxml, errm := xml.MarshalIndent(tests, "", " ")
if errm != nil {
log.Fatalf("Error: cannot format xml output: %s", errm)
}
data = append([]byte(`<?xml version="1.0" encoding="utf-8"?>`), dataxml...)
}
if v.OutputDetails == "high" {
v.PrintFunc(string(data))
}
if v.OutputResume {
v.outputResume(tests, elapsed)
}
if v.OutputDir != "" {
filename := v.OutputDir + "/" + "test_results" + "." + v.OutputFormat
if err := ioutil.WriteFile(filename, data, 0644); err != nil {
return fmt.Errorf("Error while creating file %s: %v", filename, err)
}
}
return nil
}
func outputTapFormat(tests Tests) ([]byte, error) {
tapValue := tap.New()
buf := new(bytes.Buffer)
tapValue.Writer = buf
tapValue.Header(tests.Total)
for _, ts := range tests.TestSuites {
for _, tc := range ts.TestCases {
name := ts.Name + " / " + tc.Name
if len(tc.Skipped) > 0 {
tapValue.Skip(1, name)
continue
}
if len(tc.Errors) > 0 {
tapValue.Fail(name)
for _, e := range tc.Errors {
tapValue.Diagnosticf("Error: %s", e.Value)
}
continue
}
if len(tc.Failures) > 0 {
tapValue.Fail(name)
for _, e := range tc.Failures {
tapValue.Diagnosticf("Failure: %s", e.Value)
}
continue
}
tapValue.Pass(name)
}
}
return buf.Bytes(), nil
}
func (v *Venom) outputResume(tests Tests, elapsed time.Duration) {
if v.OutputResumeFailures {
red := color.New(color.FgRed).SprintFunc()
for _, t := range tests.TestSuites {
if t.Failures > 0 || t.Errors > 0 {
v.PrintFunc("%s %s\n", red("FAILED"), t.Name)
v.PrintFunc("--------------\n")
for _, tc := range t.TestCases {
for _, f := range tc.Failures {
v.PrintFunc("%s\n", f.Value)
}
for _, f := range tc.Errors {
v.PrintFunc("%s\n", f.Value)
}
}
v.PrintFunc("-=-=-=-=-=-=-=-=-\n\n")
}
}
}
totalTestCases := 0
totalTestSteps := 0
red := color.New(color.FgRed).SprintFunc()
for _, t := range tests.TestSuites {
if t.Failures > 0 || t.Errors > 0 {
v.PrintFunc("%s %s\n", red("FAILED"), t.Name)
}
totalTestCases += len(t.TestCases)
for _, tc := range t.TestCases {
totalTestSteps += len(tc.TestSteps)
}
}
v.PrintFunc("Total:%d TotalOK:%d TotalKO:%d TotalSkipped:%d TotalTestSuite:%d TotalTestCase:%d TotalTestStep:%d Duration:%s\n",
tests.Total,
tests.TotalOK,
tests.TotalKO,
tests.TotalSkipped,
len(tests.TestSuites),
totalTestCases,
totalTestSteps,
elapsed,
)
}