-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestresult.go
63 lines (53 loc) · 1.63 KB
/
testresult.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
package kubetest
import (
"fmt"
"os"
"github.com/olekukonko/tablewriter"
)
type testResult struct {
invalid, passed, failed, total int
successRatio, threshold float64
testRuns [][]string
}
func (res *testResult) print() {
testList := tablewriter.NewWriter(os.Stdout)
testList.SetHeader([]string{"Test Type", "Spec", "Passed"})
testList.SetBorder(false)
testList.AppendBulk(res.testRuns)
fmt.Println()
testList.Render()
fmt.Println()
res.successRatio = res.calculateSuccessRatio()
data := [][]string{
{"Total", fmt.Sprintf("%d", res.total)},
{"Passed", fmt.Sprintf("%d", res.passed)},
{"Failed", fmt.Sprintf("%d", res.failed)},
{"Invalid", fmt.Sprintf("%d", res.invalid)},
{"Expected Coverage", fmt.Sprintf("%.2f", res.threshold)},
{"Actual Coverage", fmt.Sprintf("%.2f", res.successRatio)},
}
testSumamry := tablewriter.NewWriter(os.Stdout)
testSumamry.SetHeader([]string{"Tests", "Number"})
testSumamry.SetBorder(false)
testSumamry.AppendBulk(data)
fmt.Println()
testSumamry.Render()
fmt.Println()
}
func (res *testResult) addResultToRow(row int, add string) {
if res.testRuns == nil {
res.testRuns = make([][]string, 10)
}
res.testRuns[row] = append(res.testRuns[row], add)
}
func (res *testResult) checkThresholdPass() {
if res.successRatio < res.threshold {
log.Fatal(fmt.Errorf("Expected %.2f, Got %.2f", res.threshold, res.successRatio), "Failed Test Threshold")
}
}
func (res *testResult) calculateSuccessRatio() float64 {
if res.total < 1 {
log.Fatal(fmt.Errorf("Total number of tests is less than 0"), "Exiting")
}
return (float64(res.passed) / float64(res.total)) * 100
}