TestVagrant

How to Automate gRPC tests?

How to Automate gRPC tests?

Blog

How to Automate gRPC tests?

About gRPC

 

gRPC is a modern open-source high-performance Remote Procedure Call (RPC) framework that can run in any environment. It can efficiently connect services in and across the data centers with pluggable support for load balancing, tracing, health checking, and authentication.

By default, gRPC uses Protocol Buffers (or Protobuf/protos), a binary encoding format developed by Google.

For more details like about .proto file, edge over REST, and gRPC service creation and deployment can be found given link.

Lets Start gRPC Automation

  • proto is a kind of payload likewise “Json” in REST. All contracts are defined within the .proto file.
  • Generate Stub in respective language which we want to use in automation.
  • Using Stub we can do service implementation which will be used further to start the GRPC server.
  • Once the Server gets started, We can write a client in any irrespective language like Java, Golang, Python …
  • Just Import Proto file, create their own stub in the respective language
  • Start writing test code using client via passing different inputs and use the output for assertions.

Adding Dependency

Start by adding a dependency in our “build. gradle” file.

				
					plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.3.72'
    id "com.google.protobuf" version "0.8.8"
}

ext {
    grpcVersion = "1.39.0"
    protoVersion = "3.17.3"
}

dependencies {
    testImplementation group: 'org.testng', name: 'testng', version: '7.4.0'

    implementation "io.grpc:grpc-protobuf:${grpcVersion}"
    implementation "io.grpc:grpc-stub:${grpcVersion}"
    implementation "io.grpc:grpc-all:${grpcVersion}"
    implementation group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
    runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"

    testImplementation "io.grpc:grpc-testing:${grpcVersion}"
}
				
			

Adding a .proto file

“.proto” file will have all the API contracts, which means all requests, responses, and services names.

				
					syntax = "proto3";

package bookstore;

option java_package = "com.endpoints.examples.bookstore";
option java_multiple_files = true;
option java_outer_classname = "BookstoreProto";

service BookService {
  rpc GetBook (GetBookRequest) returns (BookResponse) {}
  rpc GetBooksViaAuthor (BookAuthorRequest) returns (BookResponse) {}
}

message BookResponse {
  string message = 1 ;
  string response_code = 2;
}

message GetBookRequest {
  int32 isbn = 1;
}

message BookAuthorRequest {
  string author = 1;
}
				
			

Creating a stub

“com. google. protobuf” plugin is used to create stubs in the desired language.

Run

				
					./gradlew clean build
				
			

Creating Client

We are creating a Java Client which will be interacting with gRPC services. Our Test code will interact with services via client only.

				
					public class BookClient {
    ManagedChannel channel;

    BookServiceGrpc.BookServiceBlockingStub bookServiceStub;

    public BookClient() {
        channel = ManagedChannelBuilder.forAddress("localhost", 50055).usePlaintext().build();
        bookServiceStub = BookServiceGrpc.newBlockingStub(channel);
    }

    public BookResponse getBookByISBN(Integer ISBN) throws BookNotFoundException {
        try {
            GetBookRequest getBookRequest = GetBookRequest.newBuilder().setIsbn(ISBN).build();
            return bookServiceStub.getBook(getBookRequest);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            throw new BookNotFoundException("Book not found");
        }
    }
}
				
			

Test Code

Finally, writing out test code for the gRPC service.

				
					public class BookTests extends BaseTests {
    private BookClient bookClient;

    public BookTests() {
        bookClient = new BookClient();
    }

    @Test
    public void getBookTests() throws BookNotFoundException {
        BookResponse bookResponse = bookClient.getBookByISBN(1);

        Assert.assertEquals(bookResponse.getResponseCode(), "200");
    }

    @Test
    public void getBookViaAuthor() throws AuthorNotFoundException {
        BookResponse bookResponse = bookClient.getBookByAuthor("Bob");

        Assert.assertEquals(bookResponse.getResponseCode(), "200");
    }
}
				
			

Testing gRPC using Bloomrpc

We can use BloomRPC as an alternative to test gRPC services.BloomRPC is a GUI tool, to test gRPC services likewise Postman for REST.

Share This Article

Other Related Articles

Scroll to Top