pytest-grpc plugin
pytest-grpc is a pytest plugin for testing gRPC services. It provides fixtures to start and stop gRPC servers and clients, allowing interaction with gRPC services during tests. The current stable version is 0.8.0, with releases occurring as new features are added or significant bugs are fixed, often following `pytest` or `grpcio` releases.
Common errors
-
AttributeError: 'module' object has no attribute 'YourMethod' (or similar method missing)
cause The gRPC stub or servicer defined in your `conftest.py` does not match the service definition expected by your test code, or your `.proto` files were not correctly compiled.fixVerify that your `grpc_servicer` fixture returns an instance of the correct servicer implementation and that `grpc_add_to_server` points to the correct `add_Servicer_to_server` function from your generated `_grpc.py` file. Regenerate your `.proto` files if necessary. -
grpc._channel._MultiThreadedRendezvous: <_Rendezvous of RPC that terminated with: status = StatusCode.UNAVAILABLE details = "failed to connect to all addresses" >
cause The gRPC test server failed to start or the client could not connect to it. This can happen if the `pytest_grpc_servicer_create` hook (or `grpc_servicer` and `grpc_add_to_server`) is missing or misconfigured, preventing the server from being set up.fixEnsure your `conftest.py` correctly defines `grpc_servicer` and `grpc_add_to_server` fixtures. Check for any errors during pytest startup that might indicate the server failing to bind to a port or an issue with the servicer instantiation. -
pytest.FixtureLookupError: Unknown fixture 'grpc_stub'
cause The `pytest-grpc` plugin is not installed or not active, or pytest cannot find your `conftest.py` where the plugin-required hooks are defined.fixMake sure `pytest-grpc` is installed (`pip install pytest-grpc`). Ensure your `conftest.py` is in the root of your test suite or an appropriate parent directory recognized by pytest, and that it contains the necessary `grpc_servicer` and `grpc_add_to_server` fixtures.
Warnings
- gotcha The `grpc_stub` fixture requires `pytest_grpc_servicer_create` (or directly `grpc_servicer` and `grpc_add_to_server`) to be defined in your `conftest.py`. Without these hooks, `pytest-grpc` cannot know which servicer to instantiate and register to the test server, leading to errors when trying to use `grpc_stub`.
- breaking Older versions (pre-0.6.0) might have used a `grpc_server` fixture which has been refactored. The recommended way to explicitly control server creation is now `grpc_server_factory`.
- gotcha When developing, ensure your `.proto` files are correctly compiled into Python modules (`_pb2.py` and `_pb2_grpc.py`) and are accessible on your Python path. Missing or outdated generated files can lead to import errors or `AttributeError`s.
Install
-
pip install pytest-grpc
Imports
- grpc_channel
def test_example(grpc_channel): ...
- grpc_stub
def test_example(grpc_stub): ...
- grpc_server_factory
def test_example(grpc_server_factory): ...
- grpc_add_to_server
import your_service_pb2_grpc @pytest.fixture(scope='session') def grpc_add_to_server(): return your_service_pb2_grpc.add_YourServiceServicer_to_server - grpc_servicer
import pytest class MyServicer(YourServiceServicer): ... @pytest.fixture(scope='session') def grpc_servicer(): return MyServicer()
Quickstart
# conftest.py
import pytest
import grpc
# Assuming you have compiled your_service.proto into your_service_pb2.py and your_service_pb2_grpc.py
# e.g., using 'python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. your_service.proto'
import your_service_pb2_grpc
from your_service_pb2 import HelloRequest, HelloReply
# Define your gRPC Servicer implementation
class MyTestServicer(your_service_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return HelloReply(message=f"Hello, {request.name} from test!")
# Provide the servicer instance
@pytest.fixture(scope='session')
def grpc_servicer():
return MyTestServicer()
# Provide the 'add_to_server' function from your generated _grpc module
@pytest.fixture(scope='session')
def grpc_add_to_server():
return your_service_pb2_grpc.add_GreeterServicer_to_server
# test_service.py
import your_service_pb2
def test_say_hello(grpc_stub):
# grpc_stub is an automatically created stub for your service
request = your_service_pb2.HelloRequest(name="World")
response = grpc_stub.SayHello(request)
assert response.message == "Hello, World from test!"
def test_say_hello_with_channel(grpc_channel):
# You can also create a stub manually if needed
stub = your_service_pb2_grpc.GreeterStub(grpc_channel)
request = your_service_pb2.HelloRequest(name="ChannelUser")
response = stub.SayHello(request)
assert response.message == "Hello, ChannelUser from test!"