Integration Test in Go
If you are newer at testing in Go, you can read How to Write Unit Test in Go
Intro
In this article we will be briefly talking how to write integration tests in go?” and “which tools are required for writing integration tests”.
What is integration test?
Integration test is defined as testing different and integrated modules as a group. Testing two or more integrated modules with an integration test, enables us to see if that application works properly or not.
For example, HTTP Endpoint and database executions are different modules in an app. We will be writing an integration test that will test both modules together. Our integration test will evaluate database executions business logic in HTTP Endpoint.
What do we do in this article?
- Simple API in Go contains Fiber, SQLite and PostgreSQL.
- Integration tests about API and database processes.
Prerequisites
- Basic knowledge of Go and SQL
- Go 1.14 or higher
- SQLite
- PostgreSQL
We will write a simple API.
What does API do? This API contains only one route and this route inserts new user into the database.
Route: http://localhost:3000/users (POST)
Request Payload:
{
username:"burak"
}
Create new a go program.
touch main.go && go mod init
Get these modules.
- Fiber as the web framework
- go-sqlite3 for database connection
- testify for integration tests
go get github.com/mattn/go-sqlite3
go get github.com/gofiber/fiber
go get github.com/stretchr/testify
Let’s create a project structure like the following.
├── db
│ └── db.go
├── handlers
│ └── userhandler
│ ├── users.go
├── main.go
├── models
│ └── user.go
└── repository
└── user_repo.go
db/db.go
We will create and delete the database with the following functions and we also need to connect function and create table function.
Create user model.
models/user.go
Now we can write user repository.
repository/user_repo.go
User repository has a struct named UserRepo and this struct contains database connection.
We will use Create function for creating new user.
We can connect database and we can create new user, right now.
Let’s write HTTP Endpoint.
handlers/userhandlers/users.go
User handler contains user repository, we will call user to create a function here.
main.go
We are connected to database and inject database to user repository also inject the repository to the user handler.
Now we can write integration tests.
I need 3 sections for test.
- Create database for test and users table.
- Run tests.
- Delete test database.
Let’s write the first section.
users_test.go
We will use the suite package for setup & teardown and test functions.
SetupSuite function runs before the test function.We will setup database and create the handler in SetupSuite.
TearDownSuite function runs after test function. We will delete database in TearDownSuite.
Create struct contains suite and fiber app. Suite field required, App field optional. Our tests need fiber reference.
type UsersTestSuite struct {
suite.Suite
App *fiber.App
}
func TestUsersTestSuite(t *testing.T) {
suite.Run(t, &UsersTestSuite{})
}
Now let’s write test setup. Test setup familiar to main.go.
We get database name from environment variables. This provides us with functionality. We can set real database name during production and we can set test database name during tests.
Let’s write test function. What this test function will do?
We send post request to “/users” route with user model’s payload and we wait 200 as a response status code.
And then let’s write tear down process. TearDownSuite function triggers after all tests have been run. At that point, we can delete database.
func (s *UsersTestSuite) TearDownSuite() {
err := db.DeleteDatabase()
s.Nil(err)
}
All codes can be found here: https://github.com/bdemirpolat/go-integration-test
Now you can run tests
go test ./...
Get PostgreSQL Go driver
go get github.com/lib/pq
Change db/db.go like following.
What did we do for changing SQLite to PostgreSQL?
First of all, we should change database driver, lines 5 and 11. CreateDatabase and DeleteDatabase functions have been changed into PostgreSQL and we used createdb and dropdb PostgreSQL’s command line tools. These tools come with PostgresSQL.
Now we should change insert query for PostgreSQL driver.
Change line 17 in repository/user_repo.go
From
stmt, err := u.DB.Prepare("INSERT INTO users (username) VALUES (?);")
To
stmt, err := u.DB.Prepare("INSERT INTO users (username) VALUES ($1)")
Then we should change database connection string in test file.
From
os.Setenv("DATABASE_NAME", "test_database")
To
os.Setenv("DATABASE_NAME", "postgresql://localhost:5432/integration_test?sslmode=disable")
Now you can run
go test ./...
In this article
- We wrote simple Go API and integration tests.
- We tested API with integration tests.
- We tested SQLite and PostgreSQL processes.
Resources
Thanks for reading.