test: improve testing suite and added reporting

* replace the table driven test with sub tests.
* create and publish code coverage reports via GitLab pages.
* use JUnit to view test results in the test tab in the pipeline view.
* upgrade Go to version 1.13.6.
This commit is contained in:
Dan Anglin 2020-01-18 01:04:51 +00:00
parent 0a86afbec3
commit 5fd24b80df
No known key found for this signature in database
GPG key ID: 7AC2B18EC1D09F27
5 changed files with 72 additions and 27 deletions

View file

@ -1,16 +1,40 @@
--- ---
image: golang:1.13.4 stages:
- test
- deploy
variables: variables:
CGO_ENABLED: 0 CGO_ENABLED: 0
stages:
- test
#- release
test:unit: test:unit:
stage: test artifacts:
expire_in: 30 minutes
paths:
- code-coverage.html
reports:
junit:
report.xml
before_script:
- apk add --no-cache make
- go get -u github.com/jstemmer/go-junit-report
image: golang:1.13.6-alpine
script: script:
- make test_unit - make test_cover_report
stage: test
#release pages:
artifacts:
paths:
- public
expire_in: 30 days
dependencies:
- test:unit
only:
refs:
- master@dananglin/pominal
script:
- mkdir public
- mv code-coverage.html public
stage: deploy
variables:
GIT_STRATEGY: none

View file

@ -7,7 +7,10 @@ BIN_FILE := $(BIN_DIR)/$(NAME)
all: test_unit build all: test_unit build
test_unit: test_unit:
@go test -v -cover ./... @go test -v -coverprofile=cover.out . 2>&1 | go-junit-report > report.xml
test_cover_report: test_unit
@go tool cover -html=cover.out -o code-coverage.html
build: build:
@go build -a -v -o $(BIN_FILE) @go build -a -v -o $(BIN_FILE)

View file

@ -19,6 +19,7 @@
package main package main
var pominalTestCases = []struct { var pominalTestCases = []struct {
name string
description string description string
workSession float64 workSession float64
shortBreakSession float64 shortBreakSession float64
@ -27,7 +28,8 @@ var pominalTestCases = []struct {
expectedPominal Pominal expectedPominal Pominal
}{ }{
{ {
description: "Test case 1 - Create Pominal with all valid parameters.", name: "Test case 1",
description: "When creating a Pominal value with all valid parameters.",
workSession: 1200, workSession: 1200,
shortBreakSession: 300, shortBreakSession: 300,
longBreakSession: 600, longBreakSession: 600,
@ -44,7 +46,8 @@ var pominalTestCases = []struct {
}, },
}, },
{ {
description: "Test case 2 - Create Pominal with work session set below the minimum.", name: "Test case 2",
description: "When creating a Pominal value where the work session is set below the minimum allowed time.",
workSession: 59.9999999999999999999999999999999, workSession: 59.9999999999999999999999999999999,
shortBreakSession: 1536, shortBreakSession: 1536,
longBreakSession: 1200, longBreakSession: 1200,
@ -61,7 +64,8 @@ var pominalTestCases = []struct {
}, },
}, },
{ {
description: "Test case 3 - Create Pominal with both short break and long break below the minimum.", name: "Test case 3",
description: "When creating a Pominal value where both short break and long break sessions are set below the minimum allowed time.",
workSession: 1000, workSession: 1000,
shortBreakSession: 1.23, shortBreakSession: 1.23,
longBreakSession: 45.6743, longBreakSession: 45.6743,
@ -78,7 +82,8 @@ var pominalTestCases = []struct {
}, },
}, },
{ {
description: "Test case 4 - Create Pominal with maximum work sessions set below 1.", name: "Test case 4",
description: "When creatng a Pominal where the maximum work sessions set below 1.",
workSession: 1500, workSession: 1500,
shortBreakSession: 300, shortBreakSession: 300,
longBreakSession: 1200, longBreakSession: 1200,

View file

@ -44,10 +44,12 @@ type Pominal struct {
// work is the time (represented in seconds) for the work sessions. // work is the time (represented in seconds) for the work sessions.
work float64 work float64
// shortBreak is the time (represented in seconds) for the short break sessions. // shortBreak is the time (represented in seconds)
// for the short break sessions.
shortBreak float64 shortBreak float64
// longBreak is the time (represented in seconds) for the long break sessions. // longBreak is the time (represented in seconds)
//for the long break sessions.
longBreak float64 longBreak float64
// label labels Pominal based on the session. // label labels Pominal based on the session.

View file

@ -23,8 +23,16 @@ import (
"testing" "testing"
) )
const success = "\u2713"
const failure = "\u2717"
// TestNewPominal validates the factory function for generating new
// Pominal values
func TestNewPominal(t *testing.T) { func TestNewPominal(t *testing.T) {
t.Log("Given the need to test creating a new value of type Pominal.")
{
for _, p := range pominalTestCases { for _, p := range pominalTestCases {
testFunc := func(t *testing.T) {
t.Log(p.description) t.Log(p.description)
res := NewPominal( res := NewPominal(
p.workSession, p.workSession,
@ -33,9 +41,12 @@ func TestNewPominal(t *testing.T) {
p.maxWorkSessions, p.maxWorkSessions,
) )
if !reflect.DeepEqual(res, p.expectedPominal) { if !reflect.DeepEqual(res, p.expectedPominal) {
t.Errorf("Unexpected Pominal created. Expected %v, received %v.", p.expectedPominal, res) t.Errorf("%s\tUnexpected Pominal created. Expected %v, received %v.", failure, p.expectedPominal, res)
} else { } else {
t.Log("Test passed.") t.Logf("%s\tTest passed.", success)
}
}
t.Run(p.name, testFunc)
} }
} }
} }