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

Detailed Description

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

Definition at line 127 of file __init__.py.

Public Member Functions

None __init__ (self, *, MockserverSession mockserver_session, bool experimental=False)
 
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.
 

Protected Attributes

 _mockserver_session = mockserver_session
 

Constructor & Destructor Documentation

◆ __init__()

None pytest_userver.grpc.Mockserver.__init__ ( self,
* ,
MockserverSession mockserver_session,
bool experimental = False )
Warning
This initializer is an experimental API, likely to break in the future. Consider using grpc_mockserver instead.

Initializes Mockserver.

Note
Mockserver is usually obtained from grpc_mockserver.

Definition at line 133 of file __init__.py.

Member Function Documentation

◆ __call__()

MockDecorator pytest_userver.grpc.Mockserver.__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):
@grpc_mockserver(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 146 of file __init__.py.

◆ mock_factory()

Callable[[str], MockDecorator] pytest_userver.grpc.Mockserver.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):
return grpc_mockserver.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 159 of file __init__.py.

Member Data Documentation

◆ _mockserver_session

pytest_userver.grpc.Mockserver._mockserver_session = mockserver_session
protected

Definition at line 144 of file __init__.py.


The documentation for this class was generated from the following file: