how to mock a gRPC request in pytest using monkeypatch

33 views Asked by At

I implemented a gRPC client in Python and I have functions that are connecting to a gRPC endpoint, retrieving the response and manipulating it such as:

def get_entities(self):

        with grpc.secure_channel(
            GRPC_HOST, grpc.ssl_channel_credentials()
        ) as channel:
            stub = some_service_pb2_grpc.SomeServiceStub(channel)
            request = some_service_pb2.Request()
            request.attribute = self.attribute
            
            response = stub.some_method(request,metadata=[],)

            entities = []
            for r in response:
                entities.append(MessageToDict(r)["entities"])

            entity_data = []
            for metadata in entities:
                for item in metadata:
                    entity_data.append(item)

        return entities

I want to write unit tests for these kind of functions to check their output using monkeypatch, how to mock the gRPC request and channel?

I expect the unit test to mock the gRPC request, I only want to test the code right after getting the response.

0

There are 0 answers