Skip to content

SqlTest is a test code generator for the golang. It records real DB interactions as go-sqlmock.

License

Notifications You must be signed in to change notification settings

guzenok1/go-sqltest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sqltest Build Status

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.

Installation

Once you have installed Go, run these commands to install the sqlmockgen tool:

go get github.com/guzenok1/go-sqltest
go install github.com/guzenok1/go-sqltest/sqlmockgen

Running 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/guzenok1/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/guzenok1/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.

Test function naming agreement

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.
}

About

SqlTest is a test code generator for the golang. It records real DB interactions as go-sqlmock.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published