Original address: medium.com/chewy-innov…

Original post: Medium.com/lifeatchew…

Published: September 20, 2019-5 minutes to read

Author: Shvetashva Suri, Quality Engineer II @Chewy.

Writing automation frameworks at Chewy was a great learning experience. From the beginning, I had the flexibility to explore any reasonable automation tool on the market. I was first introduced to the concept of gRPC when I started working at Chewy. This required me to be familiar with the basic principles of gRPC and learn how to write automated programs. Testing the service required understanding the protocol at the service architecture level, so I had to mine for resources. In this blog post, I summarize my approach to implementing a powerful gRPC test automation framework and how I wrote it in Java. Recently, we began the transition from gRPC to REST service contracts. On this point, I’ve also included my approach to achieving a smooth transition from the gRPC framework to the REST testing framework, using the tool REST Assured.

gRPC

GRPC is becoming a popular RPC framework, used by various modern companies like Netflix, Square, Cisco & Juniper Networks, etc. [1]. It is a remote procedure call provided by Google that defines service invocation methods on remote servers. The client creates a server stub and provides parameters and return types to these methods. The client creates a server stub and provides parameters and return types for these methods, and the server implements these methods and returns responses. The data is structured as protobuf files, you can read more about them here. Some of the advantages of gRPC are that it supports multiple languages and has higher performance than normal RPC services. More documentation on gRPC in Java, details on request and response structures can be found here.

How do we write tests for gRPC services

To test the client-side and server-side behavior of gRPC, at Chewy we wrote integration tests that implemented a request response framework similar to the one explained above. The file hierarchy includes a parent class that defines a gRPC channel, establishes it, and then defines a mock stub as required. This channel acts as a connection between the stub and the client and is used to implement server-side methods. This channel can be configured to point to any particular test environment. In this article, I define it as pointing to the local environment (port 8888). In the code snippet below, this channel is declared as a managedChannel.

public abstract class AbstractClientIntegrationTest {
    ServiceGrpc.ServiceBlockingStub stub; 
    String host = System.getProperty("DNS: / / localhost: 8888"); managedChannel = NettyChannelBuilder.forTarget(getTarget()) .build(); }Copy the code

This class is inherited by all test class files, calling gRPC server methods via stubs. As mentioned earlier, gRPC supports multiple languages and testing frameworks. In this article, I’ll use the Java language and the Junit5 framework. Below is a snippet of a sample test that makes client calls and simulates server responses.

@Test
    void test_nonNullResponse_validChannel(a) {
        ServiceProto.GetFeatureRequest request = SrviceProto.GetFeatureRequest.newBuilder().setParam("Param 1 Value").build();
        Proto.Feature featureResponse = stub.getFeature(request);
        assertNotNull(featureResponse, "The response valid request was null");
    }
Copy the code

In this test, the client calls GetFeatureRequest, a method defined on the server using ServiceProto, and sets the input parameters (represented here by setParam) in the same call. After that, the client stub implements the method and returns a response. Finally, the test uses Junit assertions to verify that the response is not empty and throws an error message if it is.

REST assertion

REST Assured is a framework for testing responses and requests to REST APIs. It is written in the Java language and behavior Driven Development (BDD) format. The Rest Assured framework can be used to parse Rest API responses to validate JSON key-value pairs or to examine critical portions of responses in XML or JSON paths. REST Assured’s literature is more readily available than the gRPC automation framework and has been mentioned in the references.

Chewy’s REST Assured testing

Before starting to write tests in the REST Assured framework, it is important to include the right dependencies in your Maven or Gradle project. Complete documentation of REST Assured Settings can be found on the official getting Started page. Any project needs the following key dependencies to set up.

  • REST-Assured
  • Json Schema validator
  • JsonPath
  • XmlPath
  • Support for Scala and Kotlin extension modules (optional).

A sample Gradle project snippet of a dependency is shown here for REST Assured’s unique dependencies. To implement REST Assured[2] effectively, we also recommend statically importing methods from REST Assured classes, some examples of which are included in the code snippet below.

testCompile group: 'io.rest-assured', name: 'rest-assured', version: '4.0.0'
testCompile group: 'io.rest-assured', name: 'json-schema-validator', version: '4.0.0'
testCompile 'IO. Rest assured: json - path: 4.0.0'
testCompile 'IO. Rest assured: XML - path: 4.0.0'
testCompile group: 'org.apache.geronimo.specs', name: '1 _spec geronimo - jms_1.', version: '1.1.1'
import static io.restassured.RestAssured.given;
import static io.restassured.matchers.RestAssuredMatchers.* ;
Copy the code

Here is a test example that shows the implementation of REST Assured framework in BDD in a Chewy format.

@Test
    public void validate_nonNullResponse(a) {
      String responseString =
                given().
                when().
                        get(url+"/? q=param1+param2").
                then().
                        assertThat()
                        .statusCode(200) 
                        log().ifError().
                extract().
                      jsonPath().get().toString();
        assertNotNull(responseString, , "The response was null"}
Copy the code

To write a test in REST Assured BDD, you can specify query parameters and URIs in a given clause, or leave the clause blank (as shown in the example). If you didn’t in the previous step, you can specify more conditions for the Rest endpoint (for example, any detailed query parameters) or specify urIs and main parameters (as in this example) in the later WHEN clause. Finally, mention the assertion you are testing in the “then” section as part of the acceptance criteria or expected test conditions. More details on the Rest Assured documentation and available libraries can be found on their website.

CI/CD implementation in Chewy

For a robust regression framework test, Chewy has implemented gRPC and REST automation frameworks. At a broad level, the tools used are Ansible Tower (AWX) for deployment, Atlassian Bamboo for CI/CD server, and GitHub for version control. To start the framework pipeline, I wrote a final task in Ansible Playbook to make a cURL call to the bamboo API. This call triggers an automated test job stationed in bamboo. Once the test runs, the pass/fail status is communicated to GitHub via an inline bamboo shell script. GitHub can then start the entire deployment process by enabling/disabling merge for the initial deployment branch.

Developing an automation framework at Chewy was arguably the most fun, stimulating, and learning experience. Looking back on this journey and seeing how far we have come fills me with a sense of achievement. I would like to pay tribute to the rest of the team, including but not limited to James, Amir, Pat, AP, Chris and Nelson, without whose help none of this would have been possible.

The resources

[1] Guide to the Creation of a guide to the creation of…

[2]John Haleby, June 3, 2016, September 2, 2019, github.com/rest-assure…

By Shvetashva Suri quality Engineer ii @Chewy


If you have any questions about Zhuo Wei’s work, please visit www.chewy.com/jobs.


www.deepl.com translation