Skip to content

Go test docker dependencies is used to declare the dependencies required to run the test with something running in docker.

Notifications You must be signed in to change notification settings

kiselev-nikolay/go-test-docker-dependencies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Go Test Docker Dependencies

Go test docker dependencies is used to declare the dependencies required to run the test with something running in docker. For example this could be a database.

Currently available:

  • PostgreSQL

    testdep.Postgres{
      Port:     5432,
      User:     "admin",
      Password: "Admin123",
      Database: "nicedb",
    }

If you are missing something else add an issue or fork this repo (PRs are welcome)!

Usage

Use go get to get this package

go get github.com/kiselev-nikolay/go-test-docker-dependencies/testdep

Import package as always

import (
	"github.com/kiselev-nikolay/go-test-docker-dependencies/testdep"
)

func TestXXX(t *testing.T) {
	pg := &testdep.Postgres{} // As example
}

Examples

Create declared database

timeoutSeconds := 5

pg := &testdep.Postgres{
	Port:     5432,
	User:     "admin",
	Password: "Admin123",
	Database: "nicedb",
}

stop, err := pg.Run(timeoutSeconds)
defer stop()

fmt.Printf("PostgreSQL connection string: %s", dockerPg.ConnString())

Use with gorm

func MustCreatePg() (string, func()) {
	dbPort, _ := testdep.FindFreePort()
	dockerPg := testdep.Postgres{
		Port:     dbPort,
		User:     "test",
		Password: "test",
		Database: "test",
	}
	stop, _ := dockerPg.Run(10)
	return dockerPg.ConnString(), func() {
		stop()
	}
}

func TestWithGorm(t *testing.T) {
	dsn, stop := MustCreatePg()
	defer stop()

	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

	// Do Gorm tests there...
}

About

Go test docker dependencies is used to declare the dependencies required to run the test with something running in docker.

Resources

Stars

Watchers

Forks

Languages