Created
April 15, 2021 08:54
-
-
Save Nydan/ec7d4f3564fc213852d4b994dd939721 to your computer and use it in GitHub Desktop.
Test Suite part for integration testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build it | |
package it | |
import ( | |
"database/sql" | |
"io/ioutil" | |
"net/http" | |
"github.com/nydan/integration_test/config" | |
"github.com/nydan/integration_test/migrator" | |
"github.com/nydan/integration_test/server" | |
"github.com/stretchr/testify/suite" | |
) | |
// exampleTestSuite is the basic testing suite. | |
// Here we can place all the dependency that we need for running the test. | |
// In this example we can add database client connection to test cases with DB. | |
type exampleTestSuite struct { | |
suite.Suite | |
db *sql.DB | |
cfg config.Config | |
srv *http.Server | |
} | |
// SetupTest initialize the testing suite dependency before the first test case executed. | |
// Within this step your integration test able to connect to any integrated client required | |
// For multiple test cases in a test suite, you may only need to initialize it once. | |
func (e *exampleTestSuite) SetupTest() { | |
// load configuration for your application | |
cfg, err := config.Load("application_cfg.toml") | |
e.NoError(err, "failed to load config") | |
e.cfg = cfg | |
// open connection to the database | |
db, err := sql.Open("postgres", e.cfg.ConnPsql) | |
e.NoError(err, "failed connect to DB") | |
e.NoError(db.Ping()) | |
e.db = db | |
// migrate the database schema | |
err = migrator.Up("/migrations") | |
e.NoError(err, "failed to migrate up the db schema") | |
// setup an http server and router | |
e.srv = server.NewHTTPServer() | |
go func() { | |
err := e.srv.ListenAndServe() | |
e.NoError(err, "failed to start HTTP server") | |
}() | |
} | |
// TearDownSuite is tearing down all the setup that has been initialize in the SetupTest. | |
// This tear down only happen at the end of suite life cycle. | |
func (e *exampleTestSuite) TearDownSuite() { | |
var err error | |
err = e.db.Close() | |
e.NoError(err, "failed to close db connection") | |
err = e.srv.Close() | |
e.NoError(err, "failed to shutdown HTTP server") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment