testit

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 6 Imported by: 0

README

TestIt

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

It allows you to avoid repetition in mock initialization, pre-test cleanup, panic avoidance.

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

Also check out github.com/sonalys/fake. A type-safe go mock generator.

Usage

To start using, you can simply create

package main

import (
	"testing"

	"github.com/sonalys/testit"
	"github.com/stretchr/testify/require"
	// ...
)

func Test_Example(test *testing.T) {
	type Dependencies struct {
		m  mocks.SomeMock
		db db.Database
	}
	type Case struct {
		ID string
	}
	type Result struct {
		u   User
		err error
	}

	t := testit.New(test, func(t *testing.T, d *Dependencies, tc *Case) func() (r Result) {
		// Pre-run initialization.
		d.m = mocks.Initialize()
		m.db = database.NewConn(&d.m)
		require.NoError(t, m.db.Cleanup())
		// Test-case run function that will execute the run behavior.
		return func() (r Result) {
			r.u, r.err = m.db.FindUser(tc.ID)
			return
		}
	})

	t.Run("case name", t.Case(
		Case{
			ID: "userID",
		},
		func(t *testing.T, d *Dependencies, tc *Case, run func() Result) {
			// Logic executed before running the test case.
			d.m.OnIsCached(func(id string) bool {
				return false
			})
			// Executes the common run function from the initial declaration.
			result := run()
			// Assertions after the test is ran.
			require.NoError(t, result.err)
			require.NotNil(t, result.u)
		},
	))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New[Dependencies, Case, Result any](t *testing.T, setup func(t *testing.T, d *Dependencies, tc *Case) func() (r Result)) *testHandler[Dependencies, Case, Result]

New initializes a new test handler.

func NoErr

func NoErr[T any](value T, err error) func(t *testing.T, msgAndArgs ...any) T

Types

type TestCase

type TestCase interface {
	// contains filtered or unexported methods
}

type TestHandler

type TestHandler[Dependencies, Case, Result any] interface {
	Run(name string, runner TestCase)
	Case(tc Case, setup ...func(t *testing.T, d *Dependencies, tc Case, run func() Result)) TestCase
}

Jump to

Keyboard shortcuts

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