Skip to content

Commit 7745ff0

Browse files
Shamus03jozefizso
authored andcommitted
Add Golang test parser
1 parent d33ca72 commit 7745ff0

File tree

10 files changed

+451
-0
lines changed

10 files changed

+451
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
![Tests failed](https://img.shields.io/badge/tests-3%20passed%2C%205%20failed%2C%201%20skipped-critical)
2+
|Report|Passed|Failed|Skipped|Time|
3+
|:---|---:|---:|---:|---:|
4+
|fixtures/golang-json.json|3 ✅|5 ❌|1 ⚪|4s|
5+
## ❌ <a id="user-content-r0" href="#user-content-r0">fixtures/golang-json.json</a>
6+
**9** tests were completed in **4s** with **3** passed, **5** failed and **1** skipped.
7+
|Test suite|Passed|Failed|Skipped|Time|
8+
|:---|---:|---:|---:|---:|
9+
|[_/home/james_t/git/test-reporter/reports/go](#user-content-r0s0)|3 ✅|5 ❌|1 ⚪|4s|
10+
### ❌ <a id="user-content-r0s0" href="#user-content-r0s0">_/home/james_t/git/test-reporter/reports/go</a>
11+
```
12+
✅ TestPassing
13+
❌ TestFailing
14+
calculator_test.go:19: expected 1+1 = 3, got 2
15+
16+
❌ TestPanicInsideFunction
17+
calculator_test.go:71: caught panic: runtime error: integer divide by zero
18+
19+
❌ TestPanicInsideTest
20+
calculator_test.go:71: caught panic: bad stuff
21+
22+
⚪ TestSkipped
23+
calculator_test.go:45: skipping test
24+
25+
❌ TestCases
26+
27+
TestCases
28+
✅ 1_+_2_=_3
29+
✅ 4_+_7_=_11
30+
❌ 2_+_3_=_4
31+
calculator_test.go:62: expected 2 + 3 = 4, got 5
32+
33+
```
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`golang-json tests report from ./reports/dotnet test results matches snapshot 1`] = `
4+
TestRunResult {
5+
"path": "fixtures/golang-json.json",
6+
"suites": [
7+
TestSuiteResult {
8+
"groups": [
9+
TestGroupResult {
10+
"name": null,
11+
"tests": [
12+
TestCaseResult {
13+
"error": undefined,
14+
"name": "TestPassing",
15+
"result": "success",
16+
"time": 90,
17+
},
18+
TestCaseResult {
19+
"error": {
20+
"details": "calculator_test.go:19: expected 1+1 = 3, got 2
21+
",
22+
"message": "calculator_test.go:19: expected 1+1 = 3, got 2
23+
",
24+
},
25+
"name": "TestFailing",
26+
"result": "failed",
27+
"time": 760,
28+
},
29+
TestCaseResult {
30+
"error": {
31+
"details": "calculator_test.go:71: caught panic: runtime error: integer divide by zero
32+
",
33+
"message": "calculator_test.go:71: caught panic: runtime error: integer divide by zero
34+
",
35+
},
36+
"name": "TestPanicInsideFunction",
37+
"result": "failed",
38+
"time": 0,
39+
},
40+
TestCaseResult {
41+
"error": {
42+
"details": "calculator_test.go:71: caught panic: bad stuff
43+
",
44+
"message": "calculator_test.go:71: caught panic: bad stuff
45+
",
46+
},
47+
"name": "TestPanicInsideTest",
48+
"result": "failed",
49+
"time": 0,
50+
},
51+
TestCaseResult {
52+
"error": {
53+
"details": "calculator_test.go:45: skipping test
54+
",
55+
"message": "calculator_test.go:45: skipping test
56+
",
57+
},
58+
"name": "TestSkipped",
59+
"result": "skipped",
60+
"time": 110,
61+
},
62+
TestCaseResult {
63+
"error": {
64+
"details": "",
65+
"message": "",
66+
},
67+
"name": "TestCases",
68+
"result": "failed",
69+
"time": 1620,
70+
},
71+
],
72+
},
73+
TestGroupResult {
74+
"name": "TestCases",
75+
"tests": [
76+
TestCaseResult {
77+
"error": undefined,
78+
"name": "1_+_2_=_3",
79+
"result": "success",
80+
"time": 540,
81+
},
82+
TestCaseResult {
83+
"error": undefined,
84+
"name": "4_+_7_=_11",
85+
"result": "success",
86+
"time": 540,
87+
},
88+
TestCaseResult {
89+
"error": {
90+
"details": "calculator_test.go:62: expected 2 + 3 = 4, got 5
91+
",
92+
"message": "calculator_test.go:62: expected 2 + 3 = 4, got 5
93+
",
94+
},
95+
"name": "2_+_3_=_4",
96+
"result": "failed",
97+
"time": 530,
98+
},
99+
],
100+
},
101+
],
102+
"name": "_/home/james_t/git/test-reporter/reports/go",
103+
"totalTime": undefined,
104+
},
105+
],
106+
"totalTime": undefined,
107+
}
108+
`;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{"Time":"2025-04-21T14:45:43.899731191-05:00","Action":"start","Package":"_/home/james_t/git/test-reporter/reports/go"}
2+
{"Time":"2025-04-21T14:45:43.907303273-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPassing"}
3+
{"Time":"2025-04-21T14:45:43.907333465-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPassing","Output":"=== RUN TestPassing\n"}
4+
{"Time":"2025-04-21T14:45:43.998551886-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPassing","Output":" calculator_test.go:11: pass!\n"}
5+
{"Time":"2025-04-21T14:45:43.998572844-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPassing","Output":"--- PASS: TestPassing (0.09s)\n"}
6+
{"Time":"2025-04-21T14:45:43.998576971-05:00","Action":"pass","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPassing","Elapsed":0.09}
7+
{"Time":"2025-04-21T14:45:43.998580617-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestFailing"}
8+
{"Time":"2025-04-21T14:45:43.998582298-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestFailing","Output":"=== RUN TestFailing\n"}
9+
{"Time":"2025-04-21T14:45:44.763532195-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestFailing","Output":" calculator_test.go:19: expected 1+1 = 3, got 2\n"}
10+
{"Time":"2025-04-21T14:45:44.763556975-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestFailing","Output":"--- FAIL: TestFailing (0.76s)\n"}
11+
{"Time":"2025-04-21T14:45:44.763560434-05:00","Action":"fail","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestFailing","Elapsed":0.76}
12+
{"Time":"2025-04-21T14:45:44.763564534-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideFunction"}
13+
{"Time":"2025-04-21T14:45:44.763566339-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideFunction","Output":"=== RUN TestPanicInsideFunction\n"}
14+
{"Time":"2025-04-21T14:45:44.763568422-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideFunction","Output":" calculator_test.go:71: caught panic: runtime error: integer divide by zero\n"}
15+
{"Time":"2025-04-21T14:45:44.763571047-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideFunction","Output":"--- FAIL: TestPanicInsideFunction (0.00s)\n"}
16+
{"Time":"2025-04-21T14:45:44.763575434-05:00","Action":"fail","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideFunction","Elapsed":0}
17+
{"Time":"2025-04-21T14:45:44.763577511-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideTest"}
18+
{"Time":"2025-04-21T14:45:44.763579734-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideTest","Output":"=== RUN TestPanicInsideTest\n"}
19+
{"Time":"2025-04-21T14:45:44.763584346-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideTest","Output":" calculator_test.go:71: caught panic: bad stuff\n"}
20+
{"Time":"2025-04-21T14:45:44.763592493-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideTest","Output":"--- FAIL: TestPanicInsideTest (0.00s)\n"}
21+
{"Time":"2025-04-21T14:45:44.763595682-05:00","Action":"fail","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestPanicInsideTest","Elapsed":0}
22+
{"Time":"2025-04-21T14:45:44.763598032-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestSkipped"}
23+
{"Time":"2025-04-21T14:45:44.763600787-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestSkipped","Output":"=== RUN TestSkipped\n"}
24+
{"Time":"2025-04-21T14:45:44.875085305-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestSkipped","Output":" calculator_test.go:45: skipping test\n"}
25+
{"Time":"2025-04-21T14:45:44.875128112-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestSkipped","Output":"--- SKIP: TestSkipped (0.11s)\n"}
26+
{"Time":"2025-04-21T14:45:44.875136234-05:00","Action":"skip","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestSkipped","Elapsed":0.11}
27+
{"Time":"2025-04-21T14:45:44.87514445-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases"}
28+
{"Time":"2025-04-21T14:45:44.87514908-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases","Output":"=== RUN TestCases\n"}
29+
{"Time":"2025-04-21T14:45:44.875158116-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/1_+_2_=_3"}
30+
{"Time":"2025-04-21T14:45:44.875163067-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/1_+_2_=_3","Output":"=== RUN TestCases/1_+_2_=_3\n"}
31+
{"Time":"2025-04-21T14:45:45.413895814-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/4_+_7_=_11"}
32+
{"Time":"2025-04-21T14:45:45.413916191-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/4_+_7_=_11","Output":"=== RUN TestCases/4_+_7_=_11\n"}
33+
{"Time":"2025-04-21T14:45:45.957801848-05:00","Action":"run","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/2_+_3_=_4"}
34+
{"Time":"2025-04-21T14:45:45.957836789-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/2_+_3_=_4","Output":"=== RUN TestCases/2_+_3_=_4\n"}
35+
{"Time":"2025-04-21T14:45:46.492673381-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/2_+_3_=_4","Output":" calculator_test.go:62: expected 2 + 3 = 4, got 5\n"}
36+
{"Time":"2025-04-21T14:45:46.492759645-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases","Output":"--- FAIL: TestCases (1.62s)\n"}
37+
{"Time":"2025-04-21T14:45:46.492779916-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/1_+_2_=_3","Output":" --- PASS: TestCases/1_+_2_=_3 (0.54s)\n"}
38+
{"Time":"2025-04-21T14:45:46.492787539-05:00","Action":"pass","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/1_+_2_=_3","Elapsed":0.54}
39+
{"Time":"2025-04-21T14:45:46.492795891-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/4_+_7_=_11","Output":" --- PASS: TestCases/4_+_7_=_11 (0.54s)\n"}
40+
{"Time":"2025-04-21T14:45:46.492801851-05:00","Action":"pass","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/4_+_7_=_11","Elapsed":0.54}
41+
{"Time":"2025-04-21T14:45:46.492806634-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/2_+_3_=_4","Output":" --- FAIL: TestCases/2_+_3_=_4 (0.53s)\n"}
42+
{"Time":"2025-04-21T14:45:46.492811453-05:00","Action":"fail","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases/2_+_3_=_4","Elapsed":0.53}
43+
{"Time":"2025-04-21T14:45:46.49281562-05:00","Action":"fail","Package":"_/home/james_t/git/test-reporter/reports/go","Test":"TestCases","Elapsed":1.62}
44+
{"Time":"2025-04-21T14:45:46.492821605-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Output":"FAIL\n"}
45+
{"Time":"2025-04-21T14:45:46.493310745-05:00","Action":"output","Package":"_/home/james_t/git/test-reporter/reports/go","Output":"FAIL\t_/home/james_t/git/test-reporter/reports/go\t2.593s\n"}
46+
{"Time":"2025-04-21T14:45:46.493343801-05:00","Action":"fail","Package":"_/home/james_t/git/test-reporter/reports/go","Elapsed":2.594}

__tests__/golang-json.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
4+
import { GolangJsonParser } from '../src/parsers/golang-json/golang-json-parser'
5+
import { ParseOptions } from '../src/test-parser'
6+
import { getReport } from '../src/report/get-report'
7+
import { normalizeFilePath } from '../src/utils/path-utils'
8+
9+
describe('golang-json tests', () => {
10+
it('report from ./reports/dotnet test results matches snapshot', async () => {
11+
const fixturePath = path.join(__dirname, 'fixtures', 'golang-json.json')
12+
const outputPath = path.join(__dirname, '__outputs__', 'golang-json.md')
13+
const filePath = normalizeFilePath(path.relative(__dirname, fixturePath))
14+
const fileContent = fs.readFileSync(fixturePath, { encoding: 'utf8' })
15+
16+
const opts: ParseOptions = {
17+
parseErrors: true,
18+
trackedFiles: ['calculator.go', 'calculator_test.go']
19+
}
20+
21+
const parser = new GolangJsonParser(opts)
22+
const result = await parser.parse(filePath, fileContent)
23+
expect(result).toMatchSnapshot()
24+
25+
const report = getReport([result])
26+
fs.mkdirSync(path.dirname(outputPath), { recursive: true })
27+
fs.writeFileSync(outputPath, report)
28+
})
29+
})

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"dart-fixture": "cd \"reports/dart\" && dart test --file-reporter=\"json:../../__tests__/fixtures/dart-json.json\"",
1818
"dotnet-fixture": "dotnet test reports/dotnet/DotnetTests.XUnitTests --logger \"trx;LogFileName=../../../../__tests__/fixtures/dotnet-trx.trx\"",
1919
"dotnet-nunit-fixture": "nunit.exe reports/dotnet/DotnetTests.NUnitV3Tests/bin/Debug/netcoreapp3.1/DotnetTests.NUnitV3Tests.dll --result=__tests__/fixtures/dotnet-nunit.xml",
20+
"dotnet-nunit-legacy-fixture": "nunit-console.exe reports/dotnet-nunit-legacy/NUnitLegacy.sln --result=__tests__/fixtures/dotnet-nunit-legacy.xml",
21+
"golang-json-fixture": "go test -v -json -timeout 5s ./reports/go | tee __tests__/fixtures/golang-json.json",
2022
"jest-fixture": "cd \"reports/jest\" && npm test",
2123
"mocha-fixture": "cd \"reports/mocha\" && npm test"
2224
},

reports/go/calculator.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "errors"
4+
5+
func CalculatorSum(a, b int) int {
6+
return a + b
7+
}
8+
9+
func CalculatorDivide(a, b int) int {
10+
return a / b
11+
}
12+
13+
var ErrDivideByZero = errors.New("divide by zero")
14+
15+
func CalculatorSafeDivide(a, b int) (int, error) {
16+
if b == 0 {
17+
return 0, ErrDivideByZero
18+
}
19+
return a / b, nil
20+
}

reports/go/calculator_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"math/rand"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestPassing(t *testing.T) {
10+
randomSleep()
11+
t.Log("pass!")
12+
}
13+
14+
func TestFailing(t *testing.T) {
15+
randomSleep()
16+
expected := 3
17+
actual := CalculatorSum(1, 1)
18+
if actual != expected {
19+
t.Fatalf("expected 1+1 = %d, got %d", expected, actual)
20+
}
21+
}
22+
23+
func TestPanicInsideFunction(t *testing.T) {
24+
defer catchPanics(t)
25+
26+
expected := 0
27+
actual := CalculatorDivide(1, 0)
28+
if actual != expected {
29+
t.Fatalf("expected 1/1 = %d, got %d", expected, actual)
30+
}
31+
}
32+
33+
func TestPanicInsideTest(t *testing.T) {
34+
defer catchPanics(t)
35+
panic("bad stuff")
36+
}
37+
38+
// Timeouts cause the entire test process to end - so we can't get good output for these
39+
// func TestTimeout(t *testing.T) {
40+
// time.Sleep(time.Second * 5)
41+
// }
42+
43+
func TestSkipped(t *testing.T) {
44+
randomSleep()
45+
t.Skipf("skipping test")
46+
}
47+
48+
func TestCases(t *testing.T) {
49+
for _, tc := range []struct {
50+
name string
51+
a, b, c int
52+
}{
53+
{"1 + 2 = 3", 1, 2, 3},
54+
{"4 + 7 = 11", 4, 7, 11},
55+
{"2 + 3 = 4", 2, 3, 4},
56+
} {
57+
t.Run(tc.name, func(t *testing.T) {
58+
randomSleep()
59+
60+
c := CalculatorSum(tc.a, tc.b)
61+
if c != tc.c {
62+
t.Fatalf("expected %s, got %d", tc.name, c)
63+
}
64+
})
65+
}
66+
}
67+
68+
func catchPanics(t *testing.T) {
69+
err := recover()
70+
if err != nil {
71+
t.Fatalf("caught panic: %v", err)
72+
}
73+
}
74+
75+
func randomSleep() {
76+
time.Sleep(time.Duration(rand.Int63n(int64(time.Second))))
77+
}

reports/go/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module test_reporter_example
2+
3+
go 1.24.2

0 commit comments

Comments
 (0)