SqlTest is a test code generator for the Go programming language. It runs your tests on real db, records sql-traffic into sqlmock and makes your tests work without real db.
Once you have installed Go, run these commands
to install the sqlmockgen
tool:
go get github.com/guzenok/go-sqltest
go install github.com/guzenok/go-sqltest/sqlmockgen
The sqlmockgen
command is used to generate offline tests code according to you special named test functions.
It takes 1 argument:
- absolute or relative import path of package to generate offline tests for;
and supports the following flags:
-
-out
: a file to which to write the resulting source code; -
-db
: a connection string to real db; -
-copyright
: copyright file used to add copyright header to the resulting source code;
Example for installed:
sqlmockgen -out=sql_test.go -db=postgresql://postgres:postgres@localhost:5432/test?sslmode=disable .
Example for gotten:
go run github.com/guzenok/go-sqltest/sqlmockgen -out=sql_test.go -db=postgresql://postgres:postgres@localhost:5432/test?sslmode=disable .
Example for go generate:
//go:generate go run github.com/guzenok/go-sqltest/sqlmockgen -out=sql_test.go -db=postgresql://postgres:postgres@localhost:5432/test?sslmode=disable .
For an example of the sqlmockgen
using, see the sample/ directory.
Your function for test db initialization (migrations, data fixtures, etc.) should be:
func initTestDb(dbUrl string) (*sql.DB, error) {
// open connection and prepare data
}
Your test functions should be like:
func test<TESTNAME>(*testing.T, *sql.DB) {
// test your code with db connection
}
(with different <TESTNAME>)
Then sqlmockgen will generate go tests:
func Test<TESTNAME>(*testing.T) {
db, err := test<TESTNAME>SqlMock()
if err != nil {
t.Fatal(err)
}
test<TESTNAME>(t, db)
}
func test<TESTNAME>SqlMock() (*sql.DB, error) {
// generated code here:
// github.com/DATA-DOG/go-sqlmock initialization.
}