Skip to content

Commit ebe312b

Browse files
committed
add more tests and parallelism in order to demo test splitting
use gotestsum to generate the result in junit xml format instead of go-junit-report, as the testcase classname value in junit xml generated by gotestsum is the fully qualified package name as required for the circleci tests split command to be able to look up the historical test timings
1 parent d28e588 commit ebe312b

File tree

10 files changed

+98
-15
lines changed

10 files changed

+98
-15
lines changed

.circleci/config.yml

+4-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ jobs: # basic units of work in a run
1010
POSTGRES_USER: circleci-demo-go
1111
POSTGRES_DB: circle_test
1212

13+
parallelism: 2
14+
1315
environment: # environment variables for the build itself
1416
TEST_RESULTS: /tmp/test-results # path to where test results will be saved
1517

@@ -26,13 +28,6 @@ jobs: # basic units of work in a run
2628
command: |
2729
go get -v
2830
29-
- run:
30-
name: Get go-junit-report for setting up test timings on CircleCI
31-
command: |
32-
go get github.com/jstemmer/go-junit-report
33-
# Remove go-junit-report from go.mod
34-
go mod tidy
35-
3631
# Wait for Postgres to be ready before proceeding
3732
- run:
3833
name: Waiting for Postgres to be ready
@@ -46,8 +41,8 @@ jobs: # basic units of work in a run
4641

4742
# store the results of our tests in the $TEST_RESULTS directory
4843
command: |
49-
trap "go-junit-report <${TEST_RESULTS}/go-test.out > ${TEST_RESULTS}/go-test-report.xml" EXIT
50-
make test | tee ${TEST_RESULTS}/go-test.out
44+
PACKAGE_NAMES=$(go list ./... | circleci tests split --split-by=timings --timings-type=classname)
45+
gotestsum --junitfile ${TEST_RESULTS}/gotestsum-report.xml -- $PACKAGE_NAMES
5146
5247
- run: make # pull and build dependencies for the project
5348

Makefile

-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
GOFILES = $(shell find . -name '*.go')
2-
GOPACKAGES = $(shell go list ./...)
32

43
default: build
54

@@ -13,8 +12,3 @@ build-native: $(GOFILES)
1312

1413
workdir/contacts: $(GOFILES)
1514
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o workdir/contacts .
16-
17-
test: test-all
18-
19-
test-all:
20-
@go test -v $(GOPACKAGES)

bigdata/processor.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package bigdata
2+
3+
import (
4+
"time"
5+
)
6+
7+
func DoComputation() bool {
8+
time.Sleep(5 * time.Second)
9+
return true
10+
}
11+
12+
func DoComplexComputation() bool {
13+
time.Sleep(10 * time.Second)
14+
return true
15+
}

bigdata/processor_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package bigdata
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestDoComputation(t *testing.T) {
10+
result := DoComputation()
11+
assert.True(t, result)
12+
}
13+
14+
func TestDoComplexComputation(t *testing.T) {
15+
result := DoComplexComputation()
16+
assert.True(t, result)
17+
}

formatter/formatter.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package formatter
2+
3+
func Format(input string) string {
4+
return "Formatted: " + input
5+
}

formatter/formatter_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package formatter
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFormat(t *testing.T) {
10+
result := Format("my string")
11+
assert.Equal(t, result, "Formatted: my string")
12+
}

math/sum.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package math
2+
3+
func Sum(x int, y int) int {
4+
return x + y
5+
}

math/sum_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package math
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestSumPositive(t *testing.T) {
11+
total := Sum(1, 1)
12+
assert.Equal(t, total, 2)
13+
}
14+
func TestSumNegative(t *testing.T) {
15+
total := Sum(1, -10)
16+
assert.Equal(t, total, -9)
17+
}
18+
19+
func TestSumLarge(t *testing.T) {
20+
time.Sleep(5 * time.Second)
21+
total := Sum(10000, 10000)
22+
assert.Equal(t, total, 20000)
23+
}

validator/validator.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package validator
2+
3+
func Validate(input string) bool {
4+
return true
5+
}

validator/validator_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package validator
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestValidate(t *testing.T) {
10+
result := Validate("my string")
11+
assert.True(t, result)
12+
}

0 commit comments

Comments
 (0)