pytest plugin for grpc
Likely abandoned. No release or commit for years (last release over 6 years ago).
$
pip install pytest-grpcpytest-grpc
Write test for gRPC with pytest.
Example
See example dir and/or read 'usage'.
Usage
For example you have some proto file with rpc declaration.
syntax = "proto3";
package test.v1;
service EchoService {
rpc handler(EchoRequest) returns (EchoResponse) {
}
}
message EchoRequest {
string name = 1;
}
message EchoResponse {
string name = 1;
}
After compile it with grpcio-tools, you get *_pb2.py and *_pb2_grpc.py files, now you can write your service.
from stub.test_pb2 import EchoRequest, EchoResponse
from stub.test_pb2_grpc import EchoServiceServicer
class Servicer(EchoServiceServicer):
def handler(self, request: EchoRequest, context) -> EchoResponse:
return EchoResponse(name=f'test-{request.name}')
def error_handler(self, request: EchoRequest, context) -> EchoResponse:
raise RuntimeError('Some error')
Point pytest with your stubs and service:
import pytest
from stub.test_pb2 import EchoRequest
@pytest.fixture(scope='module')
def grpc_add_to_server():
from stub.test_pb2_grpc import add_EchoServiceServicer_to_server
return add_EchoServiceServicer_to_server
@pytest.fixture(scope='module')
def grpc_servicer():
from servicer import Servicer
return Servicer()
@pytest.fixture(scope='module')
def grpc_stub_cls(grpc_channel):
from stub.test_pb2_grpc import EchoServiceStub
return EchoServiceStub
Write little test:
def test_some(grpc_stub):
request = EchoRequest()
response = grpc_stub.handler(request)
assert response.name == f'test-{request.name}'
def test_example(grpc_stub):
request = EchoRequest()
response = grpc_stub.error_handler(request)
assert response.name == f'test-{request.name}'
Testing secure server
from pathlib import Path
import pytest
import grpc
@pytest.fixture(scope='module')
def grpc_add_to_server():
from stub.test_pb2_grpc import add_EchoServiceServicer_to_server
return add_EchoServiceServicer_to_server
@pytest.fixture(scope='module')
def grpc_servicer():
from servicer import Servicer
return Servicer()
@pytest.fixture(scope='module')
def grpc_stub_cls(grpc_channel):
from stub.test_pb2_grpc import EchoServiceStub
return EchoServiceStub
@pytest.fixture(scope='session')
def my_ssl_key_path():
return Path('/path/to/key.pem')
@pytest.fixture(scope='session')
def my_ssl_cert_path():
return Path('/path/to/cert.pem')
@pytest.fixture(scope='module')
def grpc_server(_grpc_server, grpc_addr, my_ssl_key_path, my_ssl_cert_path):
"""
Overwrites default `grpc_server` fixture with ssl credentials
"""
credentials = grpc.ssl_server_credentials([
(my_ssl_key_path.read_bytes(),
my_ssl_cert_path.read_bytes())
])
_grpc_server.add_secure_port(grpc_addr, server_credentials=credentials)
_grpc_server.start()
yield _grpc_server
_grpc_server.stop(grace=None)
@pytest.fixture(scope='module')
def my_channel_ssl_credentials(my_ssl_cert_path):
# If we're using self-signed certificate it's necessarily to pass root certificate to channel
return grpc.ssl_channel_credentials(
root_certificates=my_ssl_cert_path.read_bytes()
)
@pytest.fixture(scope='module')
def grpc_channel(my_channel_ssl_credentials, create_channel):
"""
Overwrites default `grpc_channel` fixture with ssl credentials
"""
with create_channel(my_channel_ssl_credentials) as channel:
yield channel
@pytest.fixture(scope='module')
def grpc_authorized_channel(my_channel_ssl_credentials, create_channel):
"""
Channel with authorization header passed
"""
grpc_channel_credentials = grpc.access_token_call_credentials("some_token")
composite_credentials = grpc.composite_channel_credentials(
my_channel_ssl_credentials,
grpc_channel_credentials
)
with create_channel(composite_credentials) as channel:
yield channel
@pytest.fixture(scope='module')
def my_authorized_stub(grpc_stub_cls, grpc_channel):
"""
Stub with authorized channel
"""
return grpc_stub_cls(grpc_channel)