The accompanying code repo for the Golang Testing: Mocking and Error Handling article.
This repository contains:
- maintenance package for the acting third-party dependency package
- maintenance_test package for the related tests for the mainatenance package
- vehicle package for the target or in-development package
- vehicle_test package for the related tests for the vehicle package
- main package
The accompanying code repo for the Golang Testing: Mocking and Error Handling article.
There are two branches used for the demo:
- The (
main
branch) contains mocks, interfaces, and receiver functions where vehicle package is decoupled from the maintenance package. https://github.com/williaminfante/go_test_mocking - The (
no-mocks
branch) does NOT consider mocks, interfaces, and receiver functions where vehicle package is coupled with the maintenance package. https://github.com/williaminfante/go_test_mocking/tree/no-mocks Please note that this is not the recommended way and was only created for learning purposes.
This requires a minimum version of Go 1.16 and testify 1.7.0.
You can also just execute:
go mod tidy
To run all tests and optionally add -v
for verbose, you can just run the following the base repo path:
go test ./... -v
Or go to the folder like the maintenance
folder and run
go test -v
To check coverage to the desired output like cover.out
in the coverage
for all tests, simply:
go test ./... -v -short -coverprofile coverage/cover.out
To create an html-formatted coverage document:
go tool cover -html=coverage/cover.out -o coverage/cover.html
Recommended article Golang testing with TDD before reading the accompanying article for this code repo.
Please don't hesitate to raise an issue or submit a PR to improve this project. Thanks!