Skip to content

Commit

Permalink
Define {small,medium,all}_tests via build tags
Browse files Browse the repository at this point in the history
This enables partitioning of test suites via `go test -tags=..._tests`.

Added '{small,medium}-tests' targets to Makefile, extracted the
'static-checks' target, and redefined the 'test' target.

The 'medium-tests' command uses the '-count=1' flag to disable test
caching. This is the idiomatic way to disable test caching explicitly
per 'go help testflag'.

Updated .github/workflogs/run-tests.yaml to use '-tags=all_tests' where
appropriate and to use '-tags=small_tests' to run tests and collect
coverage. Renamed the "Test" step to "Run small tests" as well.

Added .vscode/settings.json to set "go.buildTags" to "all_tests". This
enables 'gopls' integration, "Test: Run All Tests", and other test
runner commands to continue working as normal. Hat tip for a helpful
hint to:
- https://www.ryanchapin.com/configuring-vscode-to-use-build-tags-in-golang-to-separate-integration-and-unit-test-code/
  • Loading branch information
mbland committed Apr 14, 2023
1 parent cb75894 commit 2ae3f4b
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 10 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ jobs:
go fmt ./...
result="$(git diff)" && echo "$result" && [[ -z "$result" ]]
go vet ./...
go run honnef.co/go/tools/cmd/staticcheck ./...
go run honnef.co/go/tools/cmd/staticcheck -tags=all_tests ./...
- name: Build
run: go build -v ./...
run: go build -v -tags=all_tests ./...

- name: Test
run: go test -covermode=count -coverprofile=coverage.out ./...
- name: Run small tests
run: |
go test -covermode=count -coverprofile=coverage.out \
-tags=small_tests ./...
- name: Send test coverage to Coveralls.io
uses: coverallsapp/github-action@v2
Expand Down
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.buildTags": "all_tests"
}
23 changes: 17 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
SHELL := /bin/bash
.POSIX:
.PHONY: all clean delete deploy run-local sam-build coverage test build-Function
.PHONY: all clean \
delete deploy run-local sam-build \
coverage test medium-tests small-tests static-checks\
build-Function

# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-build.html#examples-makefile-identifier
# https://docs.aws.amazon.com/lambda/latest/dg/golang-package.html
Expand All @@ -9,13 +12,21 @@ build-Function:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-s -w" \
-o $(ARTIFACTS_DIR)/main lambda/main.go

test:
go vet ./...
staticcheck ./...
go test ./...
static-checks:
go vet -tags=all_tests ./...
go run honnef.co/go/tools/cmd/staticcheck -tags=all_tests ./...
go build -tags=all_tests ./...

small-tests:
go test -tags=small_tests ./...

medium-tests:
go test -tags=medium_tests -count=1 ./...

test: static-checks small-tests medium-tests

coverage:
go test -covermode=count -coverprofile=coverage.out ./...
go test -covermode=count -coverprofile=coverage.out -tags=small_tests ./...
go tool cover -html=coverage.out

sam-build: template.yml
Expand Down
2 changes: 2 additions & 0 deletions db/database_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build medium_tests || all_tests

package db

import (
Expand Down
2 changes: 2 additions & 0 deletions email/address_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package email

import (
Expand Down
2 changes: 2 additions & 0 deletions email/mailer_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package email

import (
Expand Down
2 changes: 2 additions & 0 deletions handler/api_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package handler

import (
Expand Down
2 changes: 2 additions & 0 deletions handler/event_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package handler

import (
Expand Down
2 changes: 2 additions & 0 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package handler

import (
Expand Down
2 changes: 2 additions & 0 deletions handler/mailto_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package handler

import (
Expand Down
2 changes: 2 additions & 0 deletions handler/options_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package handler

import (
Expand Down
2 changes: 2 additions & 0 deletions handler/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package handler

import (
Expand Down
2 changes: 2 additions & 0 deletions handler/sns_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build small_tests || all_tests

package handler

import (
Expand Down

0 comments on commit 2ae3f4b

Please sign in to comment.