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.
pip install pytest-grpcVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `pytest-grpc` using `conftest.py` to provide a test servicer and its `add_to_server` function. It then shows how to write a basic test using the `grpc_stub` fixture, which automatically handles server setup and client stub creation, or using `grpc_channel` to create a stub manually. This setup assumes you have generated Python gRPC code from your `.proto` definitions.
Define `grpc_servicer` and `grpc_add_to_server` fixtures in your `conftest.py` as shown in the quickstart example, providing your gRPC servicer implementation and its `add_to_server` function.
Migrate tests using `grpc_server` to use `grpc_server_factory` if you need fine-grained control over server creation, or rely on the `grpc_channel`/`grpc_stub` fixtures which implicitly manage a server for you.
Run `python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. your_service.proto` (adjust paths as needed) to generate files. Ensure the directory containing these files is in your `PYTHONPATH` or is imported correctly.