QA Interview Hub · Knowledge base · Quizzes · Українська

API › gRPC

How a QA engineer tests a gRPC service

A gRPC service is tested with a generated client, while grpcurl or Postman stay for manual exploration. An automated test compares the status code with the expected one explicitly instead of merely checking that the call threw no exception. Validation, error codes, deadlines, streams, the message size limit and version compatibility are checked. CI runs buf lint and buf breaking, and the gateway and reflection in production are checked separately.

API · gRPC

Postman, grpcurl or code: what do you use to test a gRPC service, and what exactly do you check?

A service has just got a gRPC interface, while the team only has REST skills. The first thing you want to do is open Postman and send a request. That is fine for exploring, but automated tests that catch regressions are built differently. The lesson divides the tools by purpose and gives a list of checks, and shows by example what a test looks like that catches an error and not just a 200.

The tools and why these

  • grpcurl is "curl for gRPC". If server reflection is on, it takes the schema from there; otherwise pass -proto and -import-path. Metadata: -H.
  • Postman / Insomnia: gRPC requests with a .proto import or through reflection, handy for manual exploration and demos.
  • A generated client in the test framework. For TypeScript: @grpc/grpc-js with @grpc/proto-loader or typed code from ts-proto, Buf/Connect. This is the main option for automated tests: types catch mismatches with the schema at compile time.
  • buf: buf lint and buf breaking in CI show whether a PR broke backward compatibility of the schema.
  • Load: ghz or k6 (the k6/net/grpc module).

Where each check lives

Schema in CIbuf lint and buf breakingTests on a generated clientstatuses, deadlines, streamsGateway and browser pathgrpc-web, Gateway, status mappingLoadghz or k6 (k6/net/grpc)
Figure 1. Cheap schema checks come first, the more expensive ones later.
# list services and describe a method (needs reflection)
grpcurl -plaintext localhost:50051 list
grpcurl -plaintext localhost:50051 describe shop.v1.OrderService

# a call with metadata
grpcurl -plaintext -H 'authorization: Bearer TOKEN' \
  -d '{"id": "42"}' localhost:50051 shop.v1.OrderService/GetOrder

Worked example: a test that catches an error

The weak version only checks that the call did not throw. The strong one explicitly compares the status code with the expected one for each negative case:

import * as grpc from '@grpc/grpc-js';

const call = (req: object, meta = new grpc.Metadata()) =>
  new Promise<grpc.ServiceError | null>((resolve) =>
    client.getOrder(req, meta, { deadline: Date.now() + 2000 }, (e) => resolve(e)),
  );

test('an empty id gives INVALID_ARGUMENT', async () => {
  const err = await call({ id: '' });
  expect(err?.code).toBe(grpc.status.INVALID_ARGUMENT);
});

test('no token gives UNAUTHENTICATED', async () => {
  const err = await call({ id: '42' });
  expect(err?.code).toBe(grpc.status.UNAUTHENTICATED);
});

Such a test fails if validation is broken and the server starts returning INTERNAL or UNKNOWN on an empty id. A test that expects "just a response without an error" does not notice.

What to check

  • Positive scenarios and the values of all response fields, including defaults and optional.
  • Validation: empty, oversized, invalid fields give INVALID_ARGUMENT with correct details, not INTERNAL or UNKNOWN.
  • Error codes: NOT_FOUND, ALREADY_EXISTS, PERMISSION_DENIED versus UNAUTHENTICATED (a missing, expired or foreign token).
  • Deadlines: a slow dependency gives DEADLINE_EXCEEDED, the server does not keep working after cancellation.
  • Streaming: message order, stream completion, cancellation by the client, a break midway, an empty stream.
  • Large messages: the size limit (the receive default is usually 4 MB) gives RESOURCE_EXHAUSTED, not a server crash.
  • Compatibility: an old client with a fresh server and the other way round, unknown fields and enum values.
  • Idempotency and retries: a repeated call after UNAVAILABLE creates no duplicates.
  • Security: TLS/mTLS, authorisation at the method level, reflection turned off in production or put behind authentication.

Browser and gateways

Direct gRPC from a browser is unavailable, so grpc-web through a proxy (for example Envoy), the Connect protocol or gRPC-Gateway, which gives REST/JSON on top of the service, are used. Tests should run on the gateway too: it has its own mapping of statuses to HTTP codes and its own JSON serialisation, and a bug can live only in that layer.

Common mistakes

  • Leaving reflection on in production: anyone can see all methods and types.
  • Testing only the happy path: validation and error codes stay uncovered.
  • Ignoring the gateway: tests pass on gRPC while the browser client breaks on the status mapping.
  • Putting one call type into a load test: streaming and unary behave differently.

Check yourself

grpcurl returns "server does not support the reflection API". What do you do?

Answer

Reflection is off (normal for production). Pass the schema manually: -proto order.proto -import-path ./proto. For automated tests the schema comes from the repository anyway.

Which two tests reveal that validation was broken and the server returns INTERNAL on an empty id?

Answer

A negative test with an empty id that expects exactly INVALID_ARGUMENT, and a test of the error details (BadRequest with the field name). A "no exception" test catches nothing.

In an interview. Junior: I check manually through grpcurl or Postman with the schema. I check field values and error codes. Middle/Senior: I write automated tests on a generated client and compare statuses explicitly, set deadlines, and cover streams, message limits and version compatibility. CI runs buf lint and buf breaking, I test the gateway separately and check that reflection is not open in production.

More articles in this chapter