Reputation: 9256
Currently we able to test positive cases following way:
class AuthServer(auth_grpc.AuthServicer):
def __init__(self, *args, **kwargs):
print("Initializing auth server..")
super(AuthServer, self).__init__(*args, **kwargs)
def register(self, request, context):
return auth_messages.registerResponse(uuid="Test")
With pytest
fixtures looking following way:
@pytest.fixture(scope="session")
def server():
return AuthServer()
@pytest.fixture(scope="session")
def context():
return DummyGRPCTestContext()
In test case environment accessed following way:
def test_user_registration(server, context, user):
request = auth_messages.registerRequest(**user)
result = server.register(request, context)
print("RESULT %s " % result)
However, if we want test negative case and changing grpc servicer method to following:
def register(self, request, context):
context.set_code(grpc.StatusCode.ALREADY_EXISTS)
context.set_details("User already exists")
return auth_messages.registerResponse()
We're failing in errors, related to dummy context.
Where we can get grpc context, that can be easily plugged into test environment?
Contexts like this one look complex and not ready for plug and test.
Upvotes: 2
Views: 3402
Reputation: 2560
grpc.ServicerContext
is just an interface; within your test code you should be able to write your own implementation of it and pass that to your servicer under test.
It's true that at this time we don't provide in grpc_testing
a grpc.ServicerContext
implementation ready to be used by tests and passed to systems under test, but it's also not entirely clear that we could provide one that would be simply implemented and also valuable to a large number of tests. There's a large behavioral space of how servicers under test use grpc.ServicerContext
objects and there's another large behavioral space of how authors write tests of servicers.
Upvotes: 3