At this point, we have the gRPC service just about ready, we’ve validated the service is working as expected, but as we add more functionality to our service, we need to implement some acceptance tests to ensure that we don’t see regressions in our system over time.
In this video, we are going to be looking at implementing the first of our acceptance tests to validate our AddRocket gRPC endpoint!
Utils File
Let’s start off by creating a utils.go
file within the test/
directory. This will contain all of the common functionality between our test suites and sets us up nicely should we start to add more functionality to our service.
// +acceptance
package test
import (
"log"
"github.com/TutorialEdge/tutorial-protos/rocket/v1"
"google.golang.org/grpc"
)
func GetClient() rocket.RocketServiceClient {
var conn *grpc.ClientConn
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
if err != nil {
log.Fatalf("could not connect: %s", err)
}
rocketClient := rocket.NewRocketServiceClient(conn)
return rocketClient
}
Acceptance Tests
With this in place, let’s now implement the first of our acceptance tests.
// +acceptance
package test
import (
"context"
"testing"
"github.com/TutorialEdge/tutorial-protos/rocket/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type RocketTestSuite struct {
suite.Suite
}
func (s *RocketTestSuite) TestAddRocket() {
s.T().Run("adds a new rocket successfully", func(t *testing.T) {
client := GetClient()
resp, err := client.AddRocket(
context.Background(),
&rocket.AddRocketRequest{
Rocket: &rocket.Rocket{
Id: "ae7b0bf0-fe75-4176-b20a-3ca1371f3226",
Name: "V1",
Type: "Falcon Heavy",
},
},
)
assert.NoError(s.T(), err)
assert.Equal(s.T(), "ae7b0bf0-fe75-4176-b20a-3ca1371f3226", resp.Rocket.Id)
})
}
func TestRocketService(t *testing.T) {
suite.Run(t, new(RocketTestSuite))
}
Running Our Tests
In one terminal, let’s kick off our gRPC microservice and the postgres container using docker-compose
:
$ docker-compose up --build
Next, let’s run our acceptance tests and hit our endpoints:
$ go test ./test -tags=acceptance -v
=== RUN TestRocketService
=== RUN TestRocketService/TestAddRocket
=== RUN TestRocketService/TestAddRocket/adds_a_new_rocket_successfully
--- PASS: TestRocketService (0.02s)
--- PASS: TestRocketService/TestAddRocket (0.02s)
--- PASS: TestRocketService/TestAddRocket/adds_a_new_rocket_successfully (0.02s)
PASS
ok github.com/TutorialEdge/go-grpc-services-course/test 0.380s
Awesome, we now have the first of our acceptance tests up and running for our gRPC microservice.