Golang的gRPC测试实用程序

283 阅读1分钟

Golang的gRPC测试工具

GitHub Releases
Build Status
codecov
Go Report Card
GoDevDoc
Donate

像专家一样测试gRPC服务和客户端。

前提条件

  • Go >= 1.16

安装

go get github.com/nhatthm/grpcmock

使用方法

调用一个gRPC方法

一元方法

package main

import (
	"context"
	"time"

	"github.com/nhatthm/grpcmock"
	"google.golang.org/grpc/test/bufconn"
)

func getItem(l *bufconn.Listener, id int32) (interface{}, error) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
	defer cancel()

	out := &Item{}
	err := grpcmock.InvokeUnary(ctx, "myservice/GetItem",
		&GetItemRequest{Id: id}, out,
		grpcmock.WithHeader("Locale", "en-US"),
		grpcmock.WithBufConnDialer(l),
		grpcmock.WithInsecure(),
	)

	return out, err
}