Jump to Content

|

Testing

Notes On Testing Golang Applications

Setup Main

setup_test.go is where you do any app setup for your test. This runs before the rest of the tests and can help you mock things that need to be mocked. When you build your application, test files are ignored. It is a safe space to declare variables, override variables, etc. Each package can have a TestMain func in the setup_test.go file.

// setup_test.go in the main package

package main

// may need to declare some testing specific vars up here

var mock *config.AppConfig
var mockDB *sqlDB

func TestMain(m *testing.M) {

    // configure your testing specific app config mocks

    mockApp.Title = ""
    mockApp.DSN = ""
    // mock session, repos, pass around any extra config to internal
    packages
    newSessionManager(mockApp, mockDB)
	handlers.SetHandlerRepo(handlers.NewHandlerRepo(mockApp, sqldbconn.NewSQLDbConn(mockApp, mockDB)))
	render.NewRenderer(mockApp)

    // the rest of the config setup that runs before test


    // init TestMain
    os.Exit(m.Run())



}

Adding Context And Session To http.Request

Create a couple of helper functions to create dummy session data and add it to the request: