15

We are using grpc as the RPC protocol for all our internal system. Most of the system is written in Java.

In Java, we can use InprocessServerBuilder for unittest. However, I haven't find a similar class in Python.

Can any one provide a sample code for how to do GRPC unittest in python?

Crazymooner
  • 231
  • 1
  • 3
  • 11

3 Answers3

6

How serendipitous that you have asked this question today; our unit test framework just entered code review. So for the time being the way to test is to use the full production stack to connect your client-side and server-side code (or to violate the API and mock a lot of internal stuff) but hopefully in days to weeks the much better solution will be available to you.

  • 2
    I see the code have been merged. However, I didn't find any updated doc about the unit test. Could you please give some code example about how to using the new class? – Crazymooner Sep 07 '17 at 08:28
  • I don't know that I'll be able to get to that for a while, so in the meantime take a look at [grpcio_tests/tests/testing/](https://github.com/grpc/grpc/tree/7f6a27a4d3675adc0de263d291346aab2f46e733/src/python/grpcio_tests/tests/testing/) - the tests for the unit test framework itself are meant to look like a sample application and illustrate how to use it. – Nathaniel Manista At Google Sep 08 '17 at 12:44
  • Thank you for your work Nathan, do you think the testing framework will have any updates? I noticed all the client/server tests are skipped due to an issue in protobuf https://github.com/grpc/grpc/blob/7f6a27a4d3675adc0de263d291346aab2f46e733/src/python/grpcio_tests/tests/testing/_client_test.py#L31 – WBC Aug 17 '18 at 16:41
  • 4
    @NathanielManistaAtGoogle I see the PR was merged, but, is there some documentation that explains how is supposed to be used? – Sebastian Sep 25 '18 at 21:29
  • 2
    seems some documentation: https://grpc.io/grpc/python/grpc_testing.html but still a trivial thing. – shahid ashraf Dec 14 '18 at 06:11
  • I'm perpetually fascinated by teams of smart people who put in enormous effort to produce great code, make it public, but don't produce documentation that could actually help other people make use of it... – therightstuff Jun 29 '23 at 09:35
5

Some example code to get started:

proto

syntax = "proto3";

service MyLibrary {
    rpc Search (Request) returns (Response);
}

message Request {
    string id = 1;
}

message Response {
    string status = 1;
}

python unit test

#!/usr/bin/env python
# coding=utf-8

import unittest

from grpc import StatusCode
from grpc_testing import server_from_dictionary, strict_real_time
import mylibrary_pb2

class TestCase(unittest.TestCase):

    def __init__(self, methodName) -> None:
        super().__init__(methodName)
        
        myServicer = MyLibraryServicer()
        servicers = {
            mylibrary_pb2.DESCRIPTOR.services_by_name['MyLibrary']: myServicer
        }
        self.test_server = server_from_dictionary(
            servicers, strict_real_time())

    def test_search(self):
        request = mylibrary_pb2.Request(
            id=2,
        )
        method = self.test_server.invoke_unary_unary(
            method_descriptor=(mylibrary_pb2.DESCRIPTOR
                .services_by_name['Library']
                .methods_by_name['Search']),
            invocation_metadata={},
            request=request, timeout=1)

        response, metadata, code, details = method.termination()
        self.assertTrue(bool(response.status))
        self.assertEqual(code, StatusCode.OK)

if __name__ == '__main__':
    unittest.main()
laplasz
  • 3,359
  • 1
  • 29
  • 40
rpstw
  • 1,582
  • 14
  • 16
2

I find pytest-grpc is easy to follow and get it works in few minutes.

src: https://pypi.org/project/pytest-grpc/

echo
  • 2,666
  • 1
  • 25
  • 17