-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce test helpers and run integration tests separately
This introduces a `test` package which contains some test helpers. It also introduces a convention whereby tests with "Integration" in the name are treated as integration tests and run separately from the rest of the test suite using `-skip` and `-run` arguments to `go test` as needed. Integration tests that access an external shared resource such as Redis often need to be run serially, so providing a common pattern for them them will allow us to run non-integration tests as fast as possible.
- Loading branch information
1 parent
0fda39f
commit c590917
Showing
6 changed files
with
97 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,33 @@ set -eu | |
|
||
: "${GITHUB_ACTIONS:=}" | ||
: "${GOTESTSUM_FORMAT:=dots-v2}" | ||
: "${INTEGRATION:=}" | ||
: "${LOG_FORMAT:=development}" | ||
: "${REDIS_URL:=}" | ||
|
||
cd "$(dirname "$0")" | ||
cd .. | ||
|
||
if [ "$GITHUB_ACTIONS" = "true" ]; then | ||
GOTESTSUM_FORMAT=github-actions | ||
INTEGRATION=1 | ||
REDIS_URL=redis:// | ||
fi | ||
|
||
export GOTESTSUM_FORMAT | ||
export LOG_FORMAT | ||
export REDIS_URL | ||
|
||
# Run the tests for the entire repository. | ||
# | ||
# You can change what this does by passing paths or other arguments to | ||
# gotestsum. See https://github.com/gotestyourself/gotestsum#documentation | ||
# | ||
exec go run gotest.tools/gotestsum@v1 "$@" -- -shuffle=on -timeout=15s ./... | ||
if [ "$#" -eq 0 ]; then | ||
set -- ./... | ||
fi | ||
|
||
# Run unit tests | ||
go run gotest.tools/[email protected] -- -skip=Integration -race -shuffle=on -timeout=1s "$@" | ||
|
||
# Run integration tests | ||
if [ -z "$INTEGRATION" ]; then | ||
printf "\033[1mNote:\033[0m skipping integration tests: set INTEGRATION=1 to run them.\n" >&2 | ||
exit | ||
fi | ||
go run gotest.tools/[email protected] -- -run=Integration -p=1 -race -shuffle=on -timeout=30s "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/alicebob/miniredis/v2" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
func Context(t testing.TB) context.Context { | ||
t.Helper() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.Cleanup(cancel) | ||
|
||
return ctx | ||
} | ||
|
||
func Redis(ctx context.Context, t testing.TB) *redis.Client { | ||
t.Helper() | ||
|
||
redisURL := os.Getenv("REDIS_URL") | ||
if redisURL == "" { | ||
t.Skip("REDIS_URL is not set") | ||
} | ||
|
||
opts, err := redis.ParseURL(redisURL) | ||
if err != nil { | ||
t.Fatalf("failed to parse redis url: %v", err) | ||
} | ||
|
||
rdb := redis.NewClient(opts) | ||
t.Cleanup(func() { _ = rdb.Close() }) | ||
|
||
// Reset the database | ||
if err := rdb.FlushDB(ctx).Err(); err != nil { | ||
t.Fatal("failed to flush db") | ||
} | ||
|
||
return rdb | ||
} | ||
|
||
func MiniRedis(t testing.TB) (*miniredis.Miniredis, *redis.Client) { | ||
t.Helper() | ||
|
||
mr := miniredis.RunT(t) | ||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: mr.Addr(), | ||
}) | ||
|
||
return mr, rdb | ||
} |