mirror of
https://github.com/grafana/grafana.git
synced 2025-01-01 03:37:24 -06:00
add some mock/stub guidelines to testing guideline (#16466)
This commit is contained in:
parent
ed7ad8f6ac
commit
80a170e62f
@ -6,7 +6,7 @@ This style guide is a guide for how we want to write Go code in the future. Gene
|
||||
|
||||
|
||||
## Global state
|
||||
Global state makes testing and debugging software harder and its something we want to avoid when possible.
|
||||
Global state makes testing and debugging software harder and its something we want to avoid when possible.
|
||||
Unfortunately, there is quite a lot of global state in Grafana. The way we want to migrate away from this
|
||||
is to use the `inject` package to wire up all dependencies either in `pkg/cmd/grafana-server/main.go` or
|
||||
self registering using `registry.RegisterService` ex https://github.com/grafana/grafana/blob/master/pkg/services/cleanup/cleanup.go#L25
|
||||
@ -20,7 +20,7 @@ In the `setting` packages there are many global variables which Grafana sets at
|
||||
away from and move as much configuration as possible to the `setting.Cfg` struct and pass it around, just like the bus.
|
||||
|
||||
## Linting and formatting
|
||||
We enforce strict `gofmt` formating and use some linters on our codebase. You can find the current list of linters at https://github.com/grafana/grafana/blob/master/scripts/backend-lint.sh
|
||||
We enforce strict `gofmt` formating and use some linters on our codebase. You can find the current list of linters at https://github.com/grafana/grafana/blob/master/scripts/backend-lint.sh
|
||||
|
||||
We use [revive](https://github.com/mgechev/revive) as a go linter, and do enforce our [custom config](https://github.com/grafana/grafana/blob/master/conf/revive.toml) for it.
|
||||
|
||||
@ -28,3 +28,8 @@ We use [revive](https://github.com/mgechev/revive) as a go linter, and do enforc
|
||||
We use GoConvey for BDD/scenario based testing. Which we think is useful for testing certain chain or interactions. Ex https://github.com/grafana/grafana/blob/master/pkg/services/auth/auth_token_test.go
|
||||
|
||||
For smaller tests its preferred to use standard library testing.
|
||||
|
||||
### Mocks/Stubs
|
||||
As a general rule of thumb we try to override/replace functions/methods when mocks/stubs are needed. One common task is the need of overriding time (`time.Now()`). See usage of `getTime` variable in [code](https://github.com/grafana/grafana/blob/52c39904120fb0b98494b961be67bb47574245b1/pkg/services/auth/auth_token.go#L22) and in [test](https://github.com/grafana/grafana/blob/52c39904120fb0b98494b961be67bb47574245b1/pkg/services/auth/auth_token_test.go#L23-L26) as an example.
|
||||
|
||||
When you need to stub/mock an interface you can implement a struct that allows you to override methods on a test-by-test basis. See [stub](https://github.com/grafana/grafana/blob/52c39904120fb0b98494b961be67bb47574245b1/pkg/services/auth/testing.go) and [example usage](https://github.com/grafana/grafana/blob/52c39904120fb0b98494b961be67bb47574245b1/pkg/middleware/middleware_test.go#L153-L180).
|
||||
|
Loading…
Reference in New Issue
Block a user