testutil

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Unlicense Imports: 12 Imported by: 0

Documentation

Overview

Package testutil contains utilities for common testing patterns.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertErrorMsg

func AssertErrorMsg(t testing.TB, msg string, err error) (ok bool)

AssertErrorMsg asserts that the error is not nil and that its message is equal to msg. If msg is an empty string, AssertErrorMsg asserts that the error is nil instead.

func AssertMarshalText

func AssertMarshalText(t testing.TB, s string, v encoding.TextMarshaler) (ok bool)

AssertMarshalText checks that the implementation of v's MarshalText works in all situations and results in the string s. v must be a pointer.

See https://github.com/dominikh/go-tools/issues/911.

func AssertUnmarshalText

func AssertUnmarshalText(t testing.TB, s string, v encoding.TextUnmarshaler) (ok bool)

AssertUnmarshalText checks that the implementation of v's UnmarshalText works in all situations and results in a value deeply equal to want.

func CleanupAndRequireSuccess

func CleanupAndRequireSuccess(t testing.TB, f func() (err error))

CleanupAndRequireSuccess sets a cleanup function which checks the error returned by f and fails the test using t if there is one.

func ContextWithTimeout added in v0.23.2

func ContextWithTimeout(tb testing.TB, timeout time.Duration) (ctx context.Context)

ContextWithTimeout is a helper that creates a new context with timeout and registers ctx's cleanup with tb.Cleanup.

func DiscardLogOutput added in v0.11.1

func DiscardLogOutput(m *testing.M)

DiscardLogOutput runs tests with discarded logger output.

TODO(a.garipov): Refactor project that use this to not use a global logger.

func RequireReceive added in v0.11.1

func RequireReceive[T any](t require.TestingT, ch <-chan T, timeout time.Duration) (res T, ok bool)

RequireReceive waits until res is received from ch or until the timeout is exceeded. If the timeout is exceeded, the test is failed.

func RequireSend added in v0.11.1

func RequireSend[T any](t require.TestingT, ch chan<- T, v T, timeout time.Duration)

RequireSend waits until v is sent to ch or until the timeout is exceeded. If the timeout is exceeded, the test is failed.

func RequireTypeAssert added in v0.11.1

func RequireTypeAssert[T any](t testing.TB, v any) (res T)

RequireTypeAssert is a helper that first requires the desired type and then, if the type is correct, converts and returns the value.

Types

type PanicT added in v0.10.7

type PanicT struct{}

PanicT can be used with the helpers from package require in cases when testing.T and similar standard test helpers aren't safe for use, e.g. stub HTTP handlers and goroutines.

While this type also implements assert.TestingT, prefer to use require helper functions, since this helper panics, which immediately fails the test.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/AdguardTeam/golibs/testutil"
	"github.com/stretchr/testify/require"
)

func main() {
	sigChan := make(chan struct{})
	catchf := func(substr, msg string) {
		caught := fmt.Sprintf("%v", recover())

		// Check against the OS-independent part of the test failure message.
		fmt.Printf("%s: %t\n", msg, strings.Contains(caught, substr))

		sigChan <- struct{}{}
	}

	t := testutil.PanicT{}

	go func() {
		defer catchf("Should be true", "contains meaningful message")

		require.True(t, false)
	}()
	<-sigChan

	go func() {
		defer catchf("test failed", "contains a test failure")

		t.FailNow()
	}()
	<-sigChan

}
Output:


contains meaningful message: true
contains a test failure: true

func (PanicT) Errorf added in v0.10.7

func (PanicT) Errorf(format string, args ...any)

Errorf implements the require.TestingT interface for PanicT. It panics with an error with the given format.

func (PanicT) FailNow added in v0.10.7

func (PanicT) FailNow()

FailNow implements the require.TestingT interface for PanicT. It is assumed that it will never actually be called, since Errorf panics.

Directories

Path Synopsis
Package fakefs contains fake implementations of interfaces from package io/fs from the standard library.
Package fakefs contains fake implementations of interfaces from package io/fs from the standard library.
Package fakeio contains fake implementations of interfaces from package io from the standard library.
Package fakeio contains fake implementations of interfaces from package io from the standard library.
Package fakenet contains fake implementations of interfaces from package net from the standard library.
Package fakenet contains fake implementations of interfaces from package net from the standard library.
Package fakeservice contains fake implementations of interfaces from package service.
Package fakeservice contains fake implementations of interfaces from package service.

Jump to

Keyboard shortcuts

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