testutils

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 17 Imported by: 1

README

Test utils

Common test functions and variables.

go get -u github.com/a-novel-kit/test-utils

GitHub Actions Workflow Status codecov

GitHub repo file or directory count GitHub code size in bytes

Coverage graph

Run in external executable

Sometimes, it is useful to test a function in an isolated environment. RunCMD will run the current test in a separate executable, with a controlled environment.

package my_test

import (
  "testing"

  testutils "github.com/a-novel-kit/test-utils"
)

func TestSomething(t *testing.T) {
  testutils.RunCMD(t, &testutils.CMDConfig{
    CmdFn: func(t 8testing.T) {
      // Runs under the separate executable.
    },
    MainFn: func(t *testing.T, res *testutils.CMDResult) {
      // More information available in the documentation of testutils.CMDResult.
    }, 
    // Optional, custom environment.
    Env: []string{"FOO=BAR"},
  })
}

Note: in the above example, TestSomething will be triggered twice. It is important that the RunCMD util is only called once per test function.

GRPC utils

Helpers for testing GRPC services.

Check GRPC status

Check the error returned by a GRPC call, based on a target status. If the status targeted is codes.OK, the error must be nil.

package my_test

import (
  "testing"

  "google.golang.org/grpc/codes"

  testutils "github.com/a-novel-kit/test-utils"
)

func TestSomething(t *testing.T) {
  _, grpcErr := makeGRPCCall()
  
  // Ensure the call returned without an error.
  testutils.RequireGRPCCodesEqual(t, grpcErr, codes.OK)
}

Wait for GRPC service readiness

When running a local server for testing, this utils waits for the server to be ready before running the tests.

package my_test

import (
  "testing"

  "google.golang.org/grpc"

  testutils "github.com/a-novel-kit/test-utils"
)

func TestSomething(t *testing.T) {
  var conn *grpc.ClientConn
  
  // Init the conn...
  
  // Mark the test as error if server fails to report healthy after a certain time.
  testutils.WaitConn(t, conn)
}

Misc

Chan utils

Working with channels for test can be tedious, this package provides some helpers.

package my_test

import (
  "testing"

  "google.golang.org/grpc"

  testutils "github.com/a-novel-kit/test-utils"
)

func TestSomething(t *testing.T) {
  channel := make(chan string)
  
  // Send a message to the channel, in a separate, safe goroutine.
  testutils.SendChan(channel, "Hello")
  testutils.RequireChanEqual(t, channel, "Hello")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFieldNotFound = errors.New("field not found")
	ErrNonStructPtr  = errors.New("the first parameter must be a pointer to a struct")
)
View Source
var ErrDummy = errors.New("uwups")

ErrDummy is used as a placeholder error for tests.

Functions

func AssignPrivateField

func AssignPrivateField[T any, V any](src *T, field string, value V) error

AssignPrivateField allows to access and modify private struct fields. Because sometimes just fuck everything.

func CaptureChan

func CaptureChan[T any](channel <-chan T, dest *T) func()

CaptureChan captures the output of a channel and stores it in a pointer.

func CreateSTDCapture

func CreateSTDCapture(t *testing.T) (writer *os.File, capture func() string, err error)

CreateSTDCapture creates a new os.File writer that can be used in place of default system values (such as os.Stdout). The content written into the file can then be retrieved using the returned capture function.

Calling the capture function closes the original writer, so make sure you captured everything you need before calling it.

func ReadPrivateField

func ReadPrivateField[T any, V any](src *T, field string) (V, error)

ReadPrivateField reads the value of a private, inaccessible field.

func RequireChan

func RequireChan[T any](tb testing.TB, channel <-chan T, condition func(collect *assert.CollectT, value T))

func RequireChanC

func RequireChanC[T any](
	tb testing.TB,
	channel <-chan T,
	condition func(collect *assert.CollectT, value T),
	timeout time.Duration,
	interval time.Duration,
)

func RequireGRPCCodesEqual

func RequireGRPCCodesEqual(t *testing.T, err error, code codes.Code)

RequireGRPCCodesEqual checks if an error is nil or if it is a gRPC error with the expected code.

If the code is codes.OK, it will instead check if the error is nil.

func RunCMD

func RunCMD(t *testing.T, config *CMDConfig)

RunCMD setups a pattern to run test in controlled environments. https://stackoverflow.com/a/33404435/9021186

IMPORTANT: this must be run only once per test.

func SendChan

func SendChan[T any](channel chan<- T, value T)

SendChan sends a value to a channel, in a separate goroutine, to prevent blocking.

func WaitConn

func WaitConn(t *testing.T, conn *grpc.ClientConn)

WaitConn expects a conn to enter ready state. It returns when conn is available, otherwise it fails the test.

Types

type CMDConfig

type CMDConfig struct {
	// CmdFn is the function that will be executed under the separate process. Its result will then be passed to
	// MainFn.
	CmdFn func(t *testing.T)
	// MainFn is a controller function, that oversees the result of CmdFn. It is used as the actual test result.
	MainFn func(t *testing.T, res *CMDResult)
	// Env is a replacement env that will be available to the CmdFn.
	//
	// It is populated with os.Environ first. If present, the variable `JUST_CHECKING` will be filtered out.
	Env []string
}

CMDConfig is used to run a command in a controlled environment.

type CMDResult

type CMDResult struct {
	// Err is the error returned by exec.Command Run.
	Err error
	// Success is true if the process exited with a zero status.
	Success bool

	// STDOut is the captured standard output.
	STDOut string
	// STDErr is the captured standard error.
	STDErr string
}

CMDResult is the result of a command execution by RunCMD.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL