Skip to content

Latest commit

 

History

History
87 lines (60 loc) · 2.24 KB

File metadata and controls

87 lines (60 loc) · 2.24 KB

Cassettes

Cassettes store recorded gRPC interactions for replay during tests.

What is a Cassette?

A cassette is a file (YAML or JSON) containing recorded gRPC request/response pairs. When you run tests with grpcvcr, interactions are recorded to cassettes on the first run and replayed from cassettes on subsequent runs.

Cassette Format

Cassettes are stored as YAML by default:

interactions:
  - request:
      method: /myservice.UserService/GetUser
      body: "base64-encoded-protobuf"
      metadata:
        authorization:
          - "Bearer token123"
      target: "localhost:50051"
    response:
      body: "base64-encoded-protobuf"
      code: OK
      trailing_metadata: {}
      response_type: "myservice.user_pb2.GetUserResponse"
    rpc_type: unary
    recorded_at: "2024-01-15T10:30:00Z"

The target field records the gRPC server address (e.g. localhost:50051) the interaction was recorded against. It's set automatically from the target passed to RecordingChannel/AsyncRecordingChannel, and is useful when a project records interactions against multiple hosts (e.g. different providers) and needs to tell which one a given cassette entry came from. It's optional — cassettes recorded before this field existed simply omit it, and it has no effect on default request matching.

Cassette Location

By default, cassettes are stored in tests/cassettes/. You can customize this:

from grpcvcr import Cassette

# Explicit path
cassette = Cassette("path/to/my_cassette.yaml")

# JSON format
cassette = Cassette("path/to/my_cassette.json")

With pytest:

# Override cassette directory
pytest --grpcvcr-cassette-dir=my_cassettes/

Managing Cassettes

Updating Cassettes

To re-record all interactions:

from grpcvcr import Cassette, RecordMode

cassette = Cassette("test.yaml", record_mode=RecordMode.ALL)

Or via CLI:

pytest --grpcvcr-record=all

Committing Cassettes

Cassettes should be committed to version control. This allows:

  • Reproducible tests across machines
  • CI/CD without live services
  • Code review of expected responses

Clearing Old Cassettes

Delete cassette files to force re-recording:

rm tests/cassettes/*.yaml
pytest  # Re-records all cassettes