how do I write tests for my gRPC server? (Python)

57 views
Skip to first unread message

avi...@verloop.io

unread,
Dec 10, 2018, 1:27:32 AM12/10/18
to grpc.io
Let's say I have a simple proto:

service HelloService {
    rpc
SayHello(HelloReq) returns (HelloResp) {};
}


message
HelloReq {
   
string Name = 1;
}


message
HelloResp {
   
string Result = 1;
}



and my SayHello is implemented like:

class HelloServicer(hello_pb2_grpc.HelloServiceServicer):


   
def SayHello(self, request, context):
       
if len(request.Name) >= 10:
            msg
= 'Length of `Name` cannot be more than 10 characters'
            context
.set_details(msg)
            context
.set_code(grpc.StatusCode.INVALID_ARGUMENT)
           
return hello_pb2.HelloResp()


       
return hello_pb2.HelloResp(Result="Hey, {}!".format(request.Name))

Can anyone tell me how do I write tests for my server? I checked this repo - https://github.com/grpc/grpc/tree/master/src/python/grpcio_tests/tests/testing or the docs here - https://grpc.io/grpc/python/grpc_testing.html but I couldn't understand anything at all.

I really appreciate some help, thank you!

Eric Gribkoff

unread,
Dec 12, 2018, 11:09:11 PM12/12/18
to avi...@verloop.io, grpc.io
This was answered on Gitter earlier this week, but reposting my response here for anyone who finds this in the future:

We don't have a stand-alone example of unit testing a gRPC service (yet - I just filed grpc/grpc#17453). The APIs documented at https://grpc.io/grpc/python/grpc_testing.html are what you should be using, but the best I can do right now as far as pointing you to a "simple" usage of these is the test_successful_unary_unary method in https://github.com/grpc/grpc/blob/master/src/python/grpcio_tests/tests/testing/_server_test.py

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/c313a922-3f94-4bdb-9324-fb419b7d2c12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages