testit

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

TestIt

TestIt is a very simple, but powerful, testing framework.

It allows you to avoid repetition in the following:

  • mock initialization
  • pre-test cleanup
  • panic avoidance
  • test execution
  • assertions

It also helps you build small blocks that can be re-utilized between test cases without sharing states. Examples:

  • Setup a group of expected calls
  • Shared assertions for response fields

Usage

tools/tools.go

Create the tools/tools.go to declare testit as a development only dependency

//go:build tools

package tools

import (
	_ "github.com/sonalys/testit"
)

A very simple example:
package testit_test

import (
	"testing"

	"github.com/sonalys/testit"
)

func Test_Example(t *testing.T) {
	type dependency struct {
		// mock *mock.Mock // Mocks are automatically initialized
	}
	type tc struct{}

	setup := testit.SetupWithTestCase(func(t *testit.CHook[dependency, tc]) {
		// setup, pre-cleanup.
		// t.Dependencies.mock.EXPECT().DoSomething()
		t.After = func() {
			// teardown
		}
	})

	withHook := setup.Hook(func(t *testit.CHook[dependency, tc]) {
		// additional hook
		t.After = func() {
			// additional teardown
		}
	})

	additionalStep := func(t testit.DCTest[dependency, tc]) {}

	testfunc := func() (int, error) {
		return 0, nil
	}

	t.Run("example", withHook.Case(tc{},
		// any additional steps can be added manually as well.
		additionalStep,
		func(t testit.DCTest[dependency, tc]) {
			// test execution
			var err error
			t.NoError(err)
			t.FailNow("failing test")

			value := testit.NoErr(testfunc())
			t.Equal(0, value)
		},
	))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoMatchType     = errors.New("no match type")
	ErrNoPointer       = errors.New("must be interface")
	ErrInvalidArgument = errors.New("invalid arguments")
)
View Source
var BetterStack = true

BetterStack is set as true by default. It trims the stack for removing internals and the package module from the stack.

Functions

func DeepClone added in v0.2.0

func DeepClone[T any](v T) T

DeepClone is a function to deep clone a value. It can clone public and private values, but not private strings, as they are immutable.

func NewFactory added in v0.2.0

func NewFactory[T any](defaultState T) func(...func(*T)) T

NewFactory is a function to create a state factory. It allows you to create a valid initial state and modify it with functions. It is useful for creating test cases that verify field-by-field validation. It's also useful for centralizing the creation of a single valid struct, as validation rule changes, you only need to change it in one place.

func NoErr

func NoErr[V any](value V, err error) V

func NotPanics added in v0.2.0

func NotPanics(t *testing.T, f func())

func Setup added in v0.2.0

func Setup[D any](s func(t *Hook[D])) setup[D]

func SetupWithTestCase added in v0.2.0

func SetupWithTestCase[Dependencies, TestCase any](s func(t *CHook[Dependencies, TestCase])) withDepAndCase[Dependencies, TestCase]

Types

type CHook added in v0.2.0

type CHook[D, C any] struct {
	*require.Assertions
	*testing.T
	Dependencies *D
	Case         *C
	After        func()
}

type DCTest added in v0.2.0

type DCTest[D, C any] struct {
	*require.Assertions
	T            *testing.T
	Dependencies *D
	Case         *C
}

type DTest added in v0.2.0

type DTest[D any] struct {
	*require.Assertions
	T            *testing.T
	Dependencies *D
}

type Hook added in v0.2.0

type Hook[D any] struct {
	T            *testing.T
	Dependencies *D
	After        func()
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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