Cassette¶
The Cassette class is the core container for recording and replaying gRPC interactions.
Overview¶
A cassette stores a collection of recorded gRPC interactions (request/response pairs) and provides methods to find matching interactions during playback.
from grpcvcr import Cassette, RecordMode, RecordingChannel
# Create a cassette for recording
cassette = Cassette("tests/cassettes/my_test.yaml", record_mode=RecordMode.NEW_EPISODES)
# Use with a RecordingChannel
with RecordingChannel(cassette, "localhost:50051") as recording:
stub = MyServiceStub(recording.channel)
response = stub.GetUser(GetUserRequest(id=1))
API Reference¶
A collection of recorded gRPC interactions.
Cassettes store request/response pairs and can be saved to disk for later playback during tests. Use as a context manager to ensure changes are saved.
Example
from grpcvcr import Cassette, RecordMode, RecordingChannel
# Record interactions
with Cassette("tests/cassettes/my_test.yaml", record_mode=RecordMode.ALL) as cassette:
with RecordingChannel(cassette, "localhost:50051") as recording:
stub = MyServiceStub(recording.channel)
response = stub.GetUser(GetUserRequest(id=1))
# Playback interactions
with Cassette("tests/cassettes/my_test.yaml", record_mode=RecordMode.NONE) as cassette:
with RecordingChannel(cassette, "localhost:50051") as recording:
stub = MyServiceStub(recording.channel)
response = stub.GetUser(GetUserRequest(id=1)) # Returns recorded response
record_mode
class-attribute
instance-attribute
¶
record_mode: RecordMode = NEW_EPISODES
How to handle recording vs playback.
save ¶
Save cassette to disk if it has been modified.
Called automatically when using the cassette as a context manager.
find_interaction ¶
Find a matching recorded interaction for a request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
InteractionRequest
|
The request to match. |
required |
Returns:
| Type | Description |
|---|---|
Interaction | None
|
The matching Interaction, or None if not found. |
Source code in src/grpcvcr/cassette.py
record_interaction ¶
Record a new interaction to the cassette.
In RecordMode.ALL, existing interactions with the same request
(based on the matcher) are replaced. In other modes, new
interactions are appended.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interaction
|
Interaction
|
The interaction to record. |
required |
Source code in src/grpcvcr/cassette.py
Context manager for using a cassette.
This is a convenience wrapper around Cassette that ensures the
cassette is saved when the context exits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the cassette file. |
required |
record_mode
|
RecordMode
|
How to handle recording vs playback. |
NEW_EPISODES
|
match_on
|
Matcher | None
|
Matcher to use for finding recorded interactions. |
None
|
Yields:
| Type | Description |
|---|---|
Cassette
|
The Cassette instance. |