https://github.com/sonalys/testit
A simple and robust Go test framework
https://github.com/sonalys/testit
framework go golang test testing
Last synced: 3 months ago
JSON representation
A simple and robust Go test framework
- Host: GitHub
- URL: https://github.com/sonalys/testit
- Owner: sonalys
- License: mit
- Created: 2024-02-29T10:41:07.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-13T08:38:40.000Z (over 1 year ago)
- Last Synced: 2025-04-07T18:48:30.683Z (12 months ago)
- Topics: framework, go, golang, test, testing
- Language: Go
- Homepage:
- Size: 46.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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
//go:build tools
package tools
import (
_ "github.com/sonalys/testit"
)
```
### A very simple example:
```go
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)
},
))
}
```