Registry / testing / pytest-grpc

pytest-grpc

JSON →
library0.8.0pypypiunverified

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-grpc
INSTALL
IMPORT
SIG · PYTEST-GRPC
P
pytest-grpc
testingpythonv0.8.0
Install
2.7s avg
Import
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 30.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.7s · import 0.000s · 31MB
29MB installed
● package 29MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

grpc_channel
from pytest_grpc import grpc_channel
from pytest_grpc import grpc_channel

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.

# 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!"
Debug
Known issues
gotchaThe `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`.
fix
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.
affects: All versions
breakingOlder 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`.
fix
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.
affects: < 0.6.0
gotchaWhen 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.
fix
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.
affects: All versions
Upgrade
Version history
0.8.0latest on PyPI · released May 1, 2020
Audit
Dependencies
pytestrequiredCore testing framework, pytest-grpc is a plugin for it.
grpciorequiredRequired for gRPC communication, server, and client functionality.
Agent activity
16 hits · last 30 days
node
14
Amazon
1
Resources