From 5fd24b80df1776974ba93796962cdabb87832c33 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sat, 18 Jan 2020 01:04:51 +0000 Subject: [PATCH] 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. --- .gitlab-ci.yml | 40 ++++++++++++++++++++++++++++++++-------- Makefile | 5 ++++- cases_test.go | 13 +++++++++---- pominal.go | 6 ++++-- pominal_test.go | 35 +++++++++++++++++++++++------------ 5 files changed, 72 insertions(+), 27 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a2c0497..fc03200 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,16 +1,40 @@ --- -image: golang:1.13.4 +stages: +- test +- deploy variables: CGO_ENABLED: 0 -stages: -- test -#- release - 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: - - 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 diff --git a/Makefile b/Makefile index 83da195..920f706 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,10 @@ BIN_FILE := $(BIN_DIR)/$(NAME) all: test_unit build 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: @go build -a -v -o $(BIN_FILE) diff --git a/cases_test.go b/cases_test.go index 93a748c..b1d516c 100644 --- a/cases_test.go +++ b/cases_test.go @@ -19,6 +19,7 @@ package main var pominalTestCases = []struct { + name string description string workSession float64 shortBreakSession float64 @@ -27,7 +28,8 @@ var pominalTestCases = []struct { 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, shortBreakSession: 300, 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, shortBreakSession: 1536, 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, shortBreakSession: 1.23, 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, shortBreakSession: 300, longBreakSession: 1200, diff --git a/pominal.go b/pominal.go index d740dc4..df0982a 100644 --- a/pominal.go +++ b/pominal.go @@ -44,10 +44,12 @@ type Pominal struct { // work is the time (represented in seconds) for the work sessions. 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 - // 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 // label labels Pominal based on the session. diff --git a/pominal_test.go b/pominal_test.go index 1a65d7c..a823822 100644 --- a/pominal_test.go +++ b/pominal_test.go @@ -23,19 +23,30 @@ import ( "testing" ) +const success = "\u2713" +const failure = "\u2717" + +// TestNewPominal validates the factory function for generating new +// Pominal values func TestNewPominal(t *testing.T) { - for _, p := range pominalTestCases { - t.Log(p.description) - res := NewPominal( - p.workSession, - p.shortBreakSession, - p.longBreakSession, - p.maxWorkSessions, - ) - if !reflect.DeepEqual(res, p.expectedPominal) { - t.Errorf("Unexpected Pominal created. Expected %v, received %v.", p.expectedPominal, res) - } else { - t.Log("Test passed.") + t.Log("Given the need to test creating a new value of type Pominal.") + { + for _, p := range pominalTestCases { + testFunc := func(t *testing.T) { + t.Log(p.description) + res := NewPominal( + p.workSession, + p.shortBreakSession, + p.longBreakSession, + p.maxWorkSessions, + ) + if !reflect.DeepEqual(res, p.expectedPominal) { + t.Errorf("%s\tUnexpected Pominal created. Expected %v, received %v.", failure, p.expectedPominal, res) + } else { + t.Logf("%s\tTest passed.", success) + } + } + t.Run(p.name, testFunc) } } }