Coder Social home page Coder Social logo

testit's Introduction

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

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"
)

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",
		},
		// You can pass multiple functions at each case.
		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)
		},
	))
}

testit's People

Contributors

sonalys avatar

Stargazers

Tom avatar Abdulhafiz KAŞGARLI avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.