dbcleaner alternatives and similar packages
Based on the "Testing Frameworks" category.
Alternatively, view dbcleaner alternatives based on common mentions on social networks and blogs.
-
dockertest
Write better integration tests! Dockertest helps you boot up ephermal docker images for your Go tests with minimal work. -
gnomock
Test your code without writing mocks with ephemeral Docker containers ๐ฆ Setup popular services with just a couple lines of code โฑ๏ธ No bash, no yaml, only code ๐ป -
embedded-postgres
Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test -
gotest.tools
A collection of packages to augment the go testing package and support common patterns. -
go-testdeep
Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite -
testza
Full-featured test framework for Go! Assertions, fuzzing, input testing, output capturing, and much more! ๐ -
jsonassert
A Go test assertion library for verifying that two representations of JSON are semantically equal -
GoSpec
Testing framework for Go. Allows writing self-documenting tests/specifications, and executes them concurrently and safely isolated. [UNMAINTAINED] -
assert
:exclamation:Basic Assertion Library used along side native go testing, with building blocks for custom assertions -
fluentassert
DISCONTINUED. Extensible, type-safe, fluent assertion Go library. Do NOT use it (sic!). -
gogiven
gogiven - BDD testing framework for go that generates readable output directly from source code
CodeRabbit: AI Code Reviews for Developers

Do you think we are missing an alternative of dbcleaner or a related project?
README
DbCleaner
Clean database for testing, inspired by database_cleaner for Ruby. It uses flock syscall under the hood to make sure the test can runs in parallel without racing issues.
Basic usage
- To get the package, execute:
go get gopkg.in/khaiql/dbcleaner.v2
- To import this package, add the following line to your code:
import "gopkg.in/khaiql/dbcleaner.v2"
- To install
TestSuite
:
go get github.com/stretchr/testify
- For people who is using old version (v1.0), please change your import to
go import "gopkg.in/khaiql/dbcleaner.v1"
Options
During running test suites, there might be deadlock when 2 suites try to acquire the same table. Dbcleaner tries to mitigate the issue by providing options for retry and panic when the deadlock couldn't be resolved after excessive retries.
type Options struct {
Logger logging.Logger
LockTimeout time.Duration
NumberOfRetry int
RetryInterval time.Duration
}
type Option func(opt *Options)
// SetLogger to an instance of logging.Logger, default to Noop
func SetLogger(logger logging.Logger) Option {
return func(opt *Options) {
opt.Logger = logger
}
}
// SetLockTimeout sets timeout for locking operation, default to 10 seconds
func SetLockTimeout(d time.Duration) Option {
return func(opt *Options) {
opt.LockTimeout = d
}
}
// SetNumberOfRetry sets max retries for acquire the table, default to 5 times
func SetNumberOfRetry(t int) Option {
return func(opt *Options) {
opt.NumberOfRetry = t
}
}
// SetRetryInterval sets sleep duration between each retry, default to 10 seconds
func SetRetryInterval(d time.Duration) Option {
return func(opt *Options) {
opt.RetryInterval = d
}
}
// SetLockFileDir sets directory for lock files
func SetLockFileDir(dir string) Option {
return func(opt *Options) {
opt.LockFileDir = dir
}
}
cleaner := dbcleaner.New(SetNumberOfRetry(10), SetLockTimeout(5*time.Second))
Using with testify's suite
import (
"testing"
"gopkg.in/khaiql/dbcleaner.v2"
"gopkg.in/khaiql/dbcleaner.v2/engine"
"github.com/stretchr/testify/suite"
)
var Cleaner = dbcleaner.New()
type ExampleSuite struct {
suite.Suite
}
func (suite *ExampleSuite) SetupSuite() {
// Init and set mysql cleanup engine
mysql := engine.NewMySQLEngine("YOUR_DB_DSN")
Cleaner.SetEngine(mysql)
}
func (suite *ExampleSuite) SetupTest() {
Cleaner.Acquire("users")
}
func (suite *ExampleSuite) TearDownTest() {
Cleaner.Clean("users")
}
func (suite *ExampleSuite) TestSomething() {
// Have some meaningful test
suite.Equal(true, true)
}
func TestRunSuite(t *testing.T) {
suite.Run(t, new(ExampleSuite))
}
Support drivers
- postgres
- mysql
- sqlite3
Write cleaner for other drivers
Basically all drivers supported by database/sql
package are also supported by
dbcleaner
. Check list of drivers:
https://github.com/golang/go/wiki/SQLDrivers
For custom driver, implement your own engine.Engine
interface and call SetEngine
on dbcleaner.Cleaner
instance.
License
MIT
*Note that all licence references and agreements mentioned in the dbcleaner README section above
are relevant to that project's source code only.