mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-05-13 06:49:38 -05:00
docs: write unit testing guide
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
---
|
||||
title: "Testing"
|
||||
title: "Acceptance Testing"
|
||||
date: 2018-05-02T00:00:00+00:00
|
||||
weight: 37
|
||||
weight: 38
|
||||
geekdocRepo: https://github.com/owncloud/ocis
|
||||
geekdocEditPath: edit/master/docs/ocis/development
|
||||
geekdocFilePath: testing.md
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
---
|
||||
title: "Unit Testing"
|
||||
date: 2024-04-25T00:00:00+00:00
|
||||
weight: 37
|
||||
geekdocRepo: https://github.com/owncloud/ocis
|
||||
geekdocEditPath: edit/master/docs/ocis/development/unit-testing
|
||||
geekdocFilePath: _index.md
|
||||
---
|
||||
|
||||
{{< toc >}}
|
||||
|
||||
Go is a statically typed language, which makes it easy to write unit tests. The Go standard library provides a `testing` package that allows you to write tests for your code. The testing package provides a framework for writing tests, and the `go test` command runs the tests. Other than that there are a lot of libraries and tools available to make testing easier.
|
||||
|
||||
- [Testify](https://github.com/stretchr/testify) - A toolkit with common assertions and mocks that plays nicely with the standard library.
|
||||
- [Ginkgo](https://onsi.github.io/ginkgo/) - A BDD-style testing framework for Go.
|
||||
- [Gomega](https://onsi.github.io/gomega/) - A matcher/assertion library for Ginkgo.
|
||||
- [GoDog](https://github.com/cucumber/godog) - A Behavior-Driven Development framework for Go which uses Gherkin.
|
||||
|
||||
In ocis we generally use [Ginkgo](https://onsi.github.io/ginkgo/) framework for testing. To keep thins consistent, we would encourage you to use the same. In some cases, where you feel the need for a more verbose or more "code oriented" approach, you can also use the testing package from the standard library without ginkgo.
|
||||
|
||||
## 1 Ginkgo
|
||||
|
||||
Using a framework like [Ginkgo](https://onsi.github.io/ginkgo/) brings many advantages.
|
||||
|
||||
### Pros
|
||||
|
||||
- Provides a BDD-style syntax which makes it easier to write reusable and understandable tests
|
||||
- Together with [Gomega](https://onsi.github.io/gomega/) it provides a powerful and expressive framework with assertions in a natural language
|
||||
- Natural Language Format empowers testing in a way that resembles user interactions with the system
|
||||
- In the context of microservices it is particularly well suited to test individual services and the interactions between them
|
||||
- Offers support for asynchronous testing which makes it easier to test code that involves concurrency
|
||||
- Nested and structured containers and setup capabilities make it easy to organize tests and adhere to the DRY principle
|
||||
- Provides helpful error messages to identify and fix issues
|
||||
- Very usable for Test Driven Development following the ["Red, Green, Cleanup, Repeat"](https://en.wikipedia.org/wiki/Test-driven_development) workflow.
|
||||
|
||||
### Cons
|
||||
|
||||
- Sometimes it can be difficult to get started with
|
||||
|
||||
### Example
|
||||
|
||||
As you can see, **Ginkgo** and **Gomega** together provide the foundation to write understandable and maintainable tests which can mimic user interaction and the interactions between microservices.
|
||||
|
||||
```go
|
||||
Describe("Public Share Provider", func() {
|
||||
Context("When the user has no share permission", func() {
|
||||
BeforeEach(func() {
|
||||
// downgrade user permissions to have no share permission
|
||||
resourcePermissions.AddGrant = false
|
||||
})
|
||||
It("should return grpc invalid argument", func() {
|
||||
req := &link.CreatePublicShareRequest{}
|
||||
|
||||
res, err := provider.CreatePublicShare(ctx, req)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(res.GetStatus().GetCode()).To(Equal(rpc.Code_CODE_INVALID_ARGUMENT))
|
||||
Expect(res.GetStatus().GetMessage()).To(Equal("no share permission"))
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
### How to use it in ocis
|
||||
|
||||
{{< button relref="testing-ginkgo" size="large" >}}{{< icon "gdoc_arrow_right_alt" >}} Read more{{< /button >}}
|
||||
|
||||
## 2 Testing Package
|
||||
|
||||
For smaller straight-forward tests of some packages it might feel more natural to use the testing package that comes with the go standard library.
|
||||
|
||||
### Pros
|
||||
|
||||
- Straightforward approach
|
||||
- Naming conventions
|
||||
- Built-in tooling
|
||||
|
||||
### Cons
|
||||
|
||||
- Difficult to reuse code in larger and more complex packages
|
||||
- Difficult to create clean and isolated setups for the test steps
|
||||
- No natural language resemblance
|
||||
|
||||
|
||||
### How to use it in ocis
|
||||
|
||||
{{< button relref="testing-pkg" size="large" >}}{{< icon "gdoc_arrow_right_alt" >}} Read more{{< /button >}}
|
||||
@@ -0,0 +1,282 @@
|
||||
---
|
||||
title: "Testing with Ginkgo"
|
||||
date: 2024-04-25T00:00:00+00:00
|
||||
weight: 37
|
||||
geekdocRepo: https://github.com/owncloud/ocis
|
||||
geekdocEditPath: edit/master/docs/ocis/development/unit-testing
|
||||
geekdocFilePath: testing-ginkgo.md
|
||||
|
||||
---
|
||||
|
||||
{{< toc >}}
|
||||
|
||||
In this section we try to enable developers to write tests in ocis using Ginkgo and Gomega and explain how to mock other microservices to also cover some integration tests. The full documentation of the tools can be found on the [Ginkgo](https://onsi.github.io/ginkgo/) and [Gomega](https://onsi.github.io/gomega/) websites.
|
||||
|
||||
{{% hint type=tip icon=gdoc_link title="Reading the documentation" %}}
|
||||
This page provides only a basic introduction to get started with Ginkgo and Gomega. For more detailed information, please refer to the official documentation.
|
||||
|
||||
**Useful Links:**
|
||||
|
||||
- [Ginkgo](https://onsi.github.io/ginkgo/)
|
||||
- [Gomega](https://onsi.github.io/gomega/)
|
||||
- [Mockery](https://vektra.github.io/mockery/latest/)
|
||||
|
||||
{{% /hint %}}
|
||||
|
||||
## Prerequisites
|
||||
|
||||
To use Ginkgo, you need to install the Ginkgo CLI. You can install it using the following command:
|
||||
|
||||
```bash
|
||||
go install github.com/onsi/ginkgo/v2/ginkgo
|
||||
go get github.com/onsi/gomega/...
|
||||
```
|
||||
|
||||
## Getting Started
|
||||
|
||||
Navigate to the directory where you want to write your tests and run the following command:
|
||||
|
||||
### Bootstrap
|
||||
|
||||
```bash
|
||||
cd ocis/ocis-pkg/config/parser
|
||||
ginkgo bootstrap
|
||||
Generating ginkgo test suite bootstrap for parser in:
|
||||
parser_suite_test.go
|
||||
|
||||
```
|
||||
|
||||
This command creates a file a `parser_suite_test.go` file in the parser directory. This file contains the test suite for the parser package.
|
||||
|
||||
```go
|
||||
package parser_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Parser Suite")
|
||||
}
|
||||
```
|
||||
|
||||
Ginkgo defaults to setting up the suite as a `*_test` package to encourage you to only test the external behavior of your package, not its internal implementation details.
|
||||
|
||||
After the package `parser_test` declaration we import the ginkgo and gomega packages into the test's top-level namespace by performing a `.` dot-import. Since Ginkgo and Gomega are DSLs (Domain-specific Languages) this makes the tests more natural to read. If you prefer, you can avoid the dot-import via `ginkgo bootstrap --nodot`. Throughout this documentation we'll assume dot-imports.
|
||||
|
||||
With the bootstrap complete, you can now run your tests using the `ginkgo` command:
|
||||
|
||||
```bash
|
||||
ginkgo
|
||||
|
||||
Running Suite: Parser Suite - <loca-path>/ocis/ocis-pkg/config/parser
|
||||
===============================================================================================
|
||||
Random Seed: 1714076559
|
||||
|
||||
Will run 0 of 0 specs
|
||||
|
||||
Ran 0 of 0 Specs in 0.000 seconds
|
||||
SUCCESS! -- 0 Passed | 0 Failed | 0 Pending | 0 Skipped
|
||||
PASS
|
||||
|
||||
Ginkgo ran 1 suite in 7.0058606s
|
||||
Test Suite Passed
|
||||
```
|
||||
|
||||
Under the hood, ginkgo is simply calling `go test`. While you can run `go test` instead of the ginkgo CLI, Ginkgo has several capabilities that can only be accessed via `ginkgo`. We generally recommend users embrace the ginkgo CLI and treat it as a first-class member of their testing toolchain.
|
||||
|
||||
### Adding Specs to the Suite
|
||||
|
||||
```bash
|
||||
ginkgo generate parser
|
||||
Generating ginkgo test for Parser in: ✔ 7s 22:22:46
|
||||
parser_test.go
|
||||
```
|
||||
|
||||
This will generate a `parser_test.go` file in the parser directory. This file contains the test suite for the parser package.
|
||||
|
||||
```go
|
||||
package parser_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/config/parser"
|
||||
)
|
||||
|
||||
var _ = Describe("Parser", func() {
|
||||
|
||||
})
|
||||
```
|
||||
|
||||
## Writing Specs
|
||||
|
||||
### Describe
|
||||
|
||||
The `Describe` block is used to describe the behavior of a particular component of your code. It is a way to group together related specs. The `Describe` block takes a string and a function. The string is a description of the component you are describing, and the function contains the specs that describe the behavior of that component.
|
||||
|
||||
```go
|
||||
var _ = Describe("Parser", func() {
|
||||
// Specs go here
|
||||
})
|
||||
```
|
||||
|
||||
### Context
|
||||
|
||||
The `Context` block is used to further describe the behavior of a component. It is a way to group together related specs within a `Describe` block. The `Context` block takes a string and a function. The string is a description of the context you are describing, and the function contains the specs that describe the behavior of that context.
|
||||
|
||||
```go
|
||||
var _ = Describe("Parser", func() {
|
||||
Context("when the input is valid", func() {
|
||||
// Specs go here
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
### It
|
||||
|
||||
The `It` block is used to describe a single spec. It takes a string and a function. The string is a description of the behavior you are specifying, and the function contains the code that exercises that behavior.
|
||||
|
||||
```go
|
||||
var _ = Describe("Parser", func() {
|
||||
Context("when the input is valid", func() {
|
||||
It("parses the input", func() {
|
||||
// Spec code goes here
|
||||
})
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
### Expect
|
||||
|
||||
The `Expect` function is used to make assertions in your specs. It takes a value and returns an `*Expectation`. You can then chain methods on the `*Expectation` to make assertions about the value.
|
||||
|
||||
```go
|
||||
var _ = Describe("Parser", func() {
|
||||
Context("when the input is valid", func() {
|
||||
It("parses the input", func() {
|
||||
result := parser.Parse("valid input")
|
||||
Expect(result).To(Equal("expected output"))
|
||||
})
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
### BeforeEach
|
||||
|
||||
The `BeforeEach` block is used to run a setup function before each spec in a `Describe` or `Context` block. It takes a function that contains the setup code.
|
||||
|
||||
```go
|
||||
package parser_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/config"
|
||||
|
||||
p "github.com/owncloud/ocis/v2/ocis-pkg/config/parser"
|
||||
)
|
||||
|
||||
var _ = Describe("Parser", func() {
|
||||
var c *config.Config
|
||||
|
||||
BeforeEach(func() {
|
||||
c = config.DefaultConfig()
|
||||
})
|
||||
|
||||
Context("when the input is valid", func() {
|
||||
It("parses the input", func() {
|
||||
err := p.ParseConfig(c, false)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(c.Commons.OcisURL).To(Equal("https://localhost:9200"))
|
||||
})
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
Let us take a closer look at the code above:
|
||||
|
||||
We are following the recommended practise on variables to **"declare in container nodes"** and **"initialize in setup nodes"**. This is why we are declaring the `c` variable at the top of the `Describe` block and initializing it in the `BeforeEach` block. This is important to get isolated test steps which can be run in any order and even in parallel.
|
||||
|
||||
Let us take a look at a bad example where we are polluting the spec by not following this recommended practise:
|
||||
|
||||
```go
|
||||
package parser_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/owncloud/ocis/v2/ocis-pkg/config"
|
||||
|
||||
p "github.com/owncloud/ocis/v2/ocis-pkg/config/parser"
|
||||
)
|
||||
|
||||
|
||||
var _ = Describe("Parser", func() {
|
||||
c := config.DefaultConfig()
|
||||
|
||||
Context("when the defaults are applied", func() {
|
||||
It("fails to parse the input", func() {
|
||||
c.TokenManager.JWTSecret = "" // bam! we have changed the closure variable and it will never be reset
|
||||
err := p.ParseConfig(c, false)
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
It("parses the input", func() {
|
||||
err := p.ParseConfig(c, false)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(c.Commons.OcisURL).To(Equal("https://localhost:9200"))
|
||||
})
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
{{% hint type="warning" title="Specs MUST be clean and independent"%}}
|
||||
Always **declare variables in the container node**(which are basically `Describe()` and `Context()`)
|
||||
|
||||
and **initialize your variables in the setup nodes.** (which are basically `BeforeEach()` and `JustBeforeEach()`).
|
||||
|
||||
This will ensure that your specs are clean and independent of each other.
|
||||
{{% /hint %}}
|
||||
|
||||
### Focused Specs
|
||||
|
||||
You can focus on a single spec by adding an `F` in front of the `It` block. This will run only the focused spec.
|
||||
|
||||
```go
|
||||
var _ = Describe("Parser", func() {
|
||||
Context("when the input is valid", func() {
|
||||
FIt("parses the input", func() {
|
||||
result := parser.Parse("valid input")
|
||||
Expect(result).To(Equal("expected output"))
|
||||
})
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
### Pending Specs
|
||||
|
||||
You can mark a spec as pending by adding a `P` in front of the `It` block. This will skip the spec.
|
||||
|
||||
```go
|
||||
var _ = Describe("Parser", func() {
|
||||
Context("when the input is valid", func() {
|
||||
PIt("parses the input", func() {
|
||||
result := parser.Parse("valid input")
|
||||
Expect(result).To(Equal("expected output"))
|
||||
})
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
### Test Driven Development
|
||||
|
||||
You can run the tests in watch mode to follow a test-driven development approach. This will run the tests every time you save a file.
|
||||
|
||||
```bash
|
||||
ginkgo watch
|
||||
```
|
||||
@@ -0,0 +1,114 @@
|
||||
---
|
||||
title: "Standard Library Testing"
|
||||
date: 2024-04-25T00:00:00+00:00
|
||||
weight: 37
|
||||
geekdocRepo: https://github.com/owncloud/ocis
|
||||
geekdocEditPath: edit/master/docs/ocis/development/unit-testing
|
||||
geekdocFilePath: testing-pkg.md
|
||||
|
||||
---
|
||||
|
||||
## Using the standard library
|
||||
|
||||
To write a unit test for your package, create a file with the `_test.go` suffix. For example, if you have a package `foo` with a file `foo.go`, you can create a file `foo_test.go` in the same directory. The test file should have the same package name as the package being tested. By doing this, you can access all exported and unexported identifiers of the package. It is a good practice to keep the test file in the same package as the code being tested.
|
||||
|
||||
### Simple Example
|
||||
|
||||
We are using an oversimplified example from [FooBarQuix](https://codingdojo.org/kata/FooBarQix/) to demonstrate how to use the `testing` package.
|
||||
|
||||
```go
|
||||
package divide
|
||||
|
||||
import "strconv"
|
||||
|
||||
// If the number is divisible by 3, write "Yes" otherwise, the number
|
||||
func IsDevisible(input int) string {
|
||||
isDevisible:= (input % 3) == 0
|
||||
if isDevisible {
|
||||
return "Yes"
|
||||
}
|
||||
return strconv.Itoa(input)
|
||||
}
|
||||
```
|
||||
|
||||
To test the `IsDevisible` function, create a file `divide_test.go` in the same directory as `divide.go`. The test file should have the same package name as the package being tested.
|
||||
|
||||
A test function in Go starts with `Test` and takes `*testing.T` as the only parameter. In most cases, you will name the unit test `Test[NameOfFunction]`. The testing package provides tools to interact with the test workflow, such as `t.Errorf`, which indicates that the test failed by displaying an error message on the console.
|
||||
|
||||
The test function for the `IsDevisible` function could look like this
|
||||
|
||||
```go
|
||||
package divide
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDevide3(t *testing.T) {
|
||||
result := IsDevisible(3)
|
||||
if result != "Yes" {
|
||||
t.Errorf("Result was incorrect, got: %s, want: %s.", result, "Foo")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
To run the test, use the `go test` command in the directory where the test file is located.
|
||||
|
||||
### Use a helper package for assertions
|
||||
|
||||
You could make the test more readable by using testify. The `assert` package provides a lot of helper functions to make the test more readable.
|
||||
|
||||
```go
|
||||
package divide
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDevide3(t *testing.T) {
|
||||
result := IsDevisible(3)
|
||||
assert.Equal(t, "Yes", result)
|
||||
}
|
||||
```
|
||||
|
||||
### Table Driven Example
|
||||
|
||||
Write Table Drive Tests to test multiple inputs.
|
||||
|
||||
```go
|
||||
package divide
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
||||
func TestIsDevisibleTableDriven(t *testing.T) {
|
||||
// Defining the columns of the table
|
||||
var tests = []struct {
|
||||
name string
|
||||
input int
|
||||
want string
|
||||
}{
|
||||
// the table itself
|
||||
{"9 should be Yes", 9, "Yes"},
|
||||
{"3 should be Yes", 3, "Yes"},
|
||||
{"1 is not Yes", 1, "1"},
|
||||
{"0 should be Yes", 0, "Yes"},
|
||||
}
|
||||
|
||||
// The execution loop
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
answer := IsDevisible(tt.input)
|
||||
assert.Equal(t, tt.want, answer)
|
||||
})
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
A table-driven test starts by defining the input structure. This can be seen like defining the columns of the table. Each row of the table lists a test case to execute. Once the table is defined, the execution loop can be created.
|
||||
|
||||
The execution loop calls `t.Run()`, which defines a subtest. In our example each row of the table defines a subtest named `[NameOfTheFuction]/[NameOfTheSubTest]`.
|
||||
|
||||
This way of writing tests is very popular, and considered the canonical way to write unit tests in Go.
|
||||
Reference in New Issue
Block a user