userver: pytest_userver.plugins.grpc.mockserver.GrpcMockserver Class Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
pytest_userver.plugins.grpc.mockserver.GrpcMockserver Class Reference

Your opinion will help to improve our service

Leave a feedback >

Detailed Description

Allows to install mocks that are reset between tests, see grpc_mockserver_new.

Definition at line 189 of file mockserver.py.

Public Member Functions

MockDecorator __call__ (self, servicer_method)
 Returns a decorator to mock the specified gRPC service method implementation.
 
Callable[[str], MockDecorator] mock_factory (self, servicer_class)
 Allows to create a fixture as a shorthand for mocking methods of the specified gRPC service.
 

Member Function Documentation

◆ __call__()

MockDecorator pytest_userver.plugins.grpc.mockserver.GrpcMockserver.__call__ ( self,
servicer_method )

Returns a decorator to mock the specified gRPC service method implementation.

Example:

import samples.greeter_pb2 as greeter_protos
import samples.greeter_pb2_grpc as greeter_services
async def test_grpc_client_mock_say_hello(service_client, grpc_mockserver_new):
@grpc_mockserver_new(greeter_services.GreeterServiceServicer.SayHello)
async def mock_say_hello(request, context):
return greeter_protos.GreetingResponse(
greeting=f'Hello, {request.name} from mockserver!',
)
response = await service_client.post('/hello?case=say_hello', data='tests')
assert response.status == 200
assert 'text/plain' in response.headers['Content-Type']
assert response.text == 'Hello, tests from mockserver!'
assert mock_say_hello.times_called == 1

Definition at line 201 of file mockserver.py.

◆ mock_factory()

Callable[[str], MockDecorator] pytest_userver.plugins.grpc.mockserver.GrpcMockserver.mock_factory ( self,
servicer_class )

Allows to create a fixture as a shorthand for mocking methods of the specified gRPC service.

Example:

import pytest
import samples.greeter_pb2_grpc as greeter_services
pytest_plugins = ['pytest_userver.plugins.grpc']
@pytest.fixture
def greeter_mock(grpc_mockserver_new):
return grpc_mockserver_new.mock_factory(greeter_services.GreeterServiceServicer)
@greeter_mock('SayHello')
async def mock_say_hello(mock_request, _mock_context):
return greeter_protos.GreetingResponse(
greeting=f'Hello, {mock_request.name} from mockserver!',
)

Definition at line 214 of file mockserver.py.


The documentation for this class was generated from the following file:
  • /data/code/userver/testsuite/pytest_plugins/pytest_userver/plugins/grpc/mockserver.py