如何在Python中为GRPC服务器编写单元测试?

时间:2018-08-10 18:57:25

标签: python grpc

我想使用Python unittest为我的GRPC服务器实现编写测试。我找到了grpcio-testing软件包,但是找不到任何文档来使用它。

假设我有以下server

import helloworld_pb2
import helloworld_pb2_grpc


class Greeter(helloworld_pb2_grpc.GreeterServicer):    
    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)

如何创建一个单元测试来调用SayHello并检查响应?

4 个答案:

答案 0 :(得分:0)

您可以使用的代码元素上有inline API docstrings。归档问题,将其托管在grpc.io上,格式为https://github.com/grpc/grpc/issues/13340

答案 1 :(得分:0)

您可以在setUp时启动真实的服务器,在tearDown时停止服务器。

import unittest
from concurrent import futures


class RPCGreeterServerTest(unittest.TestCase):
    server_class = Greeter
    port = 50051

    def setUp(self):
        self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

        helloworld_pb2_grpc.add_GreeterServicer_to_server(self.server_class(), self.server)
        self.server.add_insecure_port(f'[::]:{self.port}')
        self.server.start()

    def tearDown(self):
        self.server.stop(None)

    def test_server(self):
        with grpc.insecure_channel(f'localhost:{self.port}') as channel:
            stub = helloworld_pb2_grpc.GreeterStub(channel)
            response = stub.SayHello(helloworld_pb2.HelloRequest(name='Jack'))
        self.assertEqual(response.message, 'Hello, Jack!')

答案 2 :(得分:0)

我采纳了J.C的想法并将其扩展为能够为每个测试用例创建一个假服务器(模拟)。另外,在端口0上绑定以避免端口冲突:

@contextmanager
def helloworld(cls):
    """Instantiate a helloworld server and return a stub for use in tests"""
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    helloworld_pb2_grpc.add_GreeterServicer_to_server(cls(), server)
    port = server.add_insecure_port('[::]:0')
    server.start()

    try:
        with grpc.insecure_channel('localhost:%d' % port) as channel:
            yield helloworld_pb2_grpc.GreeterStub(channel)
    finally:
        server.stop(None)


class HelloWorldTest(unittest.TestCase):
    def test_hello_name(self):
        # may do something extra for this mock if it's stateful
        class FakeHelloworld(helloworld_pb2_grpc.GreeterServicer):
            def SayHello(self, request, context):
                return helloworld_pb2.SayHelloResponse()

        with helloworld(Fakehelloworld) as stub:
            response = stub.SayHello(helloworld_pb2.HelloRequest(name='Jack'))
            self.assertEqual(response.message, 'Hello, Jack!')

答案 3 :(得分:0)

您可以尝试pytest-grpc

如果您使用的是Django,则可以看看django-grpc-framework testing