How do I create a mock context for grpc unit testing (python)

Viewed 1075

In normal grpc communications, grpc automatically creates a context to go with the request. How do I create a mock context to pass with my unit test call, so my servicing function has an object to manipulate?

Currently I have the code skip the context calls if it receives a NoneType as context, but that is not a good solution.

1 Answers

You can use the magic mock library unittest.mock. And mock over the client RPC multicallable object grpc.UnaryUnaryMultiCallable (see ref), so it returns your response and context. This object is generated by our ProtoBuf plugin, and sits in the foo_pb2_grpc.py file.

Related