Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need Guidance for Intregration Testing with the new .Net Core 3.0 grpc service

I am playing around with the new GRPC Service for .Net Core 3.0 template. I have successfully created the Hello World service. I am now trying to add a test project to this solution.

I can see how to make an in-memory server for integration testing for a regular HTTP Client/Server .Net Core service. But I can find no example or guidance anywhere on the net for when using grpc protodef client/server. The issue appears to be with the CreateClient() method which returns a plain old HTTP client, and not a grpc one.

It would be good if a barebones integration test was included with the project created by the VS 2019 template. The main reason for me to look at Microservices and in particular using grpc was the idea that I can have small blocks that are easily testable with an automated testing framework. But I seem to have fallen at the first hurdle.

[Added]

Below is the simple test class. It uses NUnit. The issue I have is finding a way to run the in-memory TestHost, the same way as I do for a .Net Core API or MVC app to allow integration testing.

namespace Project.Tests
{
    public class Tests 
    {
        private Greeter.GreeterClient _client;
        private TestServer _server;

        [OneTimeSetUp]
        public void OneTimeSetup()
        {
            var server = new TestServer(WebHost.CreateDefaultBuilder()
                .UseStartup<TestStartup>()
                .UseEnvironment("Development")
                .UseUrls("https://localhost:5001"));
        }

        [SetUp]
        public void Setup()
        {
            var channel = GrpcChannel.ForAddress("https://localhost:5001");
            _client = new Greeter.GreeterClient(channel);
        }

        [Test]
        public async Task Test1()
        {
            var reply = await _client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
            Assert.IsTrue(reply.Message == "Hello GreeterClient");
        }
    }
}
like image 462
Mark LFT Avatar asked Oct 06 '19 07:10

Mark LFT


People also ask

How do I test gRPC services?

Integration testing: The gRPC app is hosted in TestServer, an in-memory test server from the Microsoft. AspNetCore. TestHost package. gRPC services are tested by calling them using a gRPC client from a unit testing library.

What is gRPC service in .NET core?

gRPC is a language agnostic, high-performance Remote Procedure Call (RPC) framework. The main benefits of gRPC are: Modern, high-performance, lightweight RPC framework. Contract-first API development, using Protocol Buffers by default, allowing for language agnostic implementations.


1 Answers

Could you please try to pass an HTTPClient created by test server into GRPC channel?

It works when I using WebApplicationFactory, haven't tested for a test server before.

var channel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
{
    HttpClient = _server.CreateClient()
});
            
var client = new Greeter.GreeterClient(channel);
            
var response await client.SayHelloAsync(new HelloRequest());

// Assert anything you want
like image 107
Bao To Quoc Avatar answered Sep 22 '22 11:09

Bao To Quoc