Integration testing
In Go, the main difference between integration testing and unit testing is what you test, but there is no real difference between the tools you use. You can use the same testing package and similar testing code, but instead of testing a small chunk of isolated code, you test parts of the system together.
For example, we can test the Login function again, but this time, we will use the real thing instead of mocking the database. Let’s see how we can do that:
func TestLogin(t *testing.T) {
app := NewApp()
user := User{Username: "user", Password: "password", SessionToken: "123123"}
app.db.InsertUser(user)
defer app.db.DeleteUser(user.Username)
t.Run("invalid user", func(t *testing.T) {
_, err := app.Login("invalid-username", "password")
if err == nil {
t.Errorf("Login(invalid-username, password) error = %v;
want not nil", err)
}
})
t...