From 6ea7b84b36da5ac9c896d40933089ce2bc465772 Mon Sep 17 00:00:00 2001 From: Wilfried OLLIVIER Date: Tue, 30 Apr 2019 17:09:45 +0200 Subject: [PATCH] Add golang testing tips --- src/SUMMARY.md | 2 + src/dev/golang/main.md | 1 + src/dev/golang/testing.md | 79 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/dev/golang/main.md create mode 100644 src/dev/golang/testing.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index a92152d..ffa868d 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -3,6 +3,8 @@ [Introduction](./structure/intro.md) - [Dev](./dev/main.md) + - [Golang](./dev/golang/main.md) + - [Testing](./dev/golang/testing.md) - [Web](./dev/web/main.md) - [front](./dev/web/front/main.md) - [babel and babel-preset-stage-2](./dev/web/front/babel-preset-stage-2.md) diff --git a/src/dev/golang/main.md b/src/dev/golang/main.md new file mode 100644 index 0000000..34d4e35 --- /dev/null +++ b/src/dev/golang/main.md @@ -0,0 +1 @@ +# Golang diff --git a/src/dev/golang/testing.md b/src/dev/golang/testing.md new file mode 100644 index 0000000..f033e22 --- /dev/null +++ b/src/dev/golang/testing.md @@ -0,0 +1,79 @@ +# Testing tips, golang edition + +[Documentation](https://golang.org/pkg/testing/) + +## Files + +Test file should be place in the package directory and should be name using +the following convention : + +- repo : github.com/go/pkg +- package : github.com/go/pkg/example +- package file : example/pkg.go +- test file : exemple/pkg_test.go + +## Run + +```shell +go test github.com/go/pkg/package +``` + +### fmt.Println is not working + +Gniagniagnia, use + +```golang +t.Log() +``` + +or + +```golang +t.Logf() +``` +also, + +```shell +go test github.com/go/pkg/package -v +``` + +## How to fail + +### Mark test as failed (next tests executed) + +```golang +t.Fail() +``` +### Mark test as failed AND exit + +```golang +t.FailNow() +``` + +### Print and mark test as failed + +```golang +t.Error() +``` + +or + +```golang +t.Errorf() +``` + +### Print, mark test as failed AND exit + +```golang +t.Fatal() +``` + +or + +```golang +t.Fatalf() +``` + +## I don't want my tests to be messy (kudos @athoune) + +[Assert](https://godoc.org/github.com/stretchr/testify/assert)