2019-04-30 17:09:45 +02:00
|
|
|
# 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)
|
2019-05-02 21:18:51 +02:00
|
|
|
|
|
|
|
## Shit, i want to disable my oauth test on my CI
|
|
|
|
|
|
|
|
```golang
|
|
|
|
if os.Getenv("DRONE") == "true" {
|
|
|
|
t.Skip("Skipping test in CI environment")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Short mode
|
|
|
|
|
|
|
|
This test is too long ? Skip it !
|
|
|
|
|
|
|
|
```golang
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping test in short mode")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```shell
|
|
|
|
go test github.com/go/pkg/package --short
|
|
|
|
```
|