Skip to content

Commit

Permalink
docs(GO provider): Add section for unit testing (#2490)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspoignant authored Oct 9, 2024
1 parent 212bc4d commit 453bdb1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions website/docs/openfeature_sdk/server_providers/openfeature_go.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,35 @@ if adminFlag {
}
```

## Unit testing

When testing code that relies on feature flags, it's crucial to have control over the flag values to simulate different scenarios and isolate your tests.

The **OpenFeature in-memory provider** offers a convenient solution by allowing you to define and manipulate feature flag values directly within your tests.
This eliminates the need to start a relay-proxy and provides a more efficient and flexible testing environment.

In this example, we demonstrate how to use the in-memory provider to set the value of 2 different feature flags in a test:

```go
func TestMyTest(t *testing.T) {
apiInstance := openfeature.GetApiInstance()
apiInstance.SetProvider(
memprovider.NewInMemoryProvider(
map[string]memprovider.InMemoryFlag{
"my-flag-1": {Key: "my-flag-1", DefaultVariant: "var_a", Variants: map[string]any{"var_a": true}},
"my-flag-2": {Key: "my-flag-2", DefaultVariant: "disabled", Variants: map[string]any{"disabled": false}},
}
))

// Your test code here
}
```
By setting the in memory provider you are able to predict the value of the flag you will get when running your tests.
This is a good way to ensure that your test is using the value you are expecting as response of your feature flag.

:::note
The in memory provider is part of the OpenFeature SDK, you don't need to export any extra dependencies to use it.
:::

## Contribute to the provider
You can find the source of the provider in the [`open-feature/go-sdk-contrib`](https://github.com/open-feature/go-sdk-contrib/tree/main/providers/go-feature-flag) repository.
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,35 @@ if adminFlag {
}
```

## Unit testing

When testing code that relies on feature flags, it's crucial to have control over the flag values to simulate different scenarios and isolate your tests.

The **OpenFeature in-memory provider** offers a convenient solution by allowing you to define and manipulate feature flag values directly within your tests.
This eliminates the need to start a relay-proxy and provides a more efficient and flexible testing environment.

In this example, we demonstrate how to use the in-memory provider to set the value of 2 different feature flags in a test:

```go
func TestMyTest(t *testing.T) {
apiInstance := openfeature.GetApiInstance()
apiInstance.SetProvider(
memprovider.NewInMemoryProvider(
map[string]memprovider.InMemoryFlag{
"my-flag-1": {Key: "my-flag-1", DefaultVariant: "var_a", Variants: map[string]any{"var_a": true}},
"my-flag-2": {Key: "my-flag-2", DefaultVariant: "disabled", Variants: map[string]any{"disabled": false}},
}
))

// Your test code here
}
```
By setting the in memory provider you are able to predict the value of the flag you will get when running your tests.
This is a good way to ensure that your test is using the value you are expecting as response of your feature flag.

:::note
The in memory provider is part of the OpenFeature SDK, you don't need to export any extra dependencies to use it.
:::

## Contribute to the provider
You can find the source of the provider in the [`open-feature/go-sdk-contrib`](https://github.com/open-feature/go-sdk-contrib/tree/main/providers/go-feature-flag) repository.

0 comments on commit 453bdb1

Please sign in to comment.