test

package
v5.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package test implements a testing framwork, and provides default tests.

Deprecated: This package is deprecated in favor of go-micro.dev/v5/testing. Use the testing.Harness for a cleaner, more maintainable approach. See test/DEPRECATED.md for migration guide.

Package test provides utilities for testing micro services.

Due to go-micro's global defaults, running multiple services in one process requires careful isolation. This package provides helpers for the common case of testing a single service.

Basic usage:

func TestUserService(t *testing.T) {
    h := test.NewHarness(t)
    defer h.Stop()

    // Register your service handler
    h.Register(new(UsersHandler))

    // Start the harness
    h.Start()

    // Call the service
    var rsp UserResponse
    err := h.Call("Users.Create", &CreateRequest{Name: "Alice"}, &rsp)
    if err != nil {
        t.Fatal(err)
    }
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoTests returns no test params are set.
	ErrNoTests = errors.New("No tests to run, all values set to 0")
)

Functions

func RunBenchmark

func RunBenchmark(b *testing.B, name string, service micro.Service, test testFunc,
	cancel context.CancelFunc, started chan struct{})

RunBenchmark will run benchmarks on a provided service.

A test function can be provided that will be fun b.N times.

Types

type Harness added in v5.15.0

type Harness struct {
	// contains filtered or unexported fields
}

Harness provides an in-process test environment for a micro service

func NewHarness added in v5.15.0

func NewHarness(t *testing.T) *Harness

NewHarness creates a new test harness

func (*Harness) AssertCallFails added in v5.15.0

func (h *Harness) AssertCallFails(endpoint string, req, rsp interface{})

AssertCallFails checks that a call fails

func (*Harness) AssertCallSucceeds added in v5.15.0

func (h *Harness) AssertCallSucceeds(endpoint string, req, rsp interface{})

AssertCallSucceeds checks that a call succeeds

func (*Harness) AssertServiceRunning added in v5.15.0

func (h *Harness) AssertServiceRunning()

AssertServiceRunning checks that the service is registered

func (*Harness) Call added in v5.15.0

func (h *Harness) Call(endpoint string, req, rsp interface{}) error

Call invokes a service method

func (*Harness) CallContext added in v5.15.0

func (h *Harness) CallContext(ctx context.Context, endpoint string, req, rsp interface{}) error

CallContext invokes a service method with context

func (*Harness) Client added in v5.15.0

func (h *Harness) Client() client.Client

Client returns the test client for advanced usage

func (*Harness) Name added in v5.15.0

func (h *Harness) Name(name string) *Harness

Name sets the service name (default: "test")

func (*Harness) Register added in v5.15.0

func (h *Harness) Register(handler interface{}) *Harness

Register sets the handler for the service

func (*Harness) Registry added in v5.15.0

func (h *Harness) Registry() registry.Registry

Registry returns the test registry for advanced usage

func (*Harness) Server added in v5.15.0

func (h *Harness) Server() server.Server

Server returns the test server for advanced usage

func (*Harness) Start added in v5.15.0

func (h *Harness) Start()

Start starts the service

func (*Harness) Stop added in v5.15.0

func (h *Harness) Stop()

Stop stops the service

type ServiceTestConfig

type ServiceTestConfig struct {
	// Service name to use for the tests
	Name string
	// NewService function will be called to setup the new service.
	// It takes in a list of options, which by default will Context and an
	// AfterStart with channel to signal when the service has been started.
	NewService func(name string, opts ...micro.Option) (micro.Service, error)
	// Parallel is the number of prallell routines to use for the tests.
	Parallel []int
	// Sequential is the number of sequential requests to send per parallel process.
	Sequential []int
	// Streams is the nummber of streaming messages to send over the stream per routine.
	Streams []int
	// PubSub is the number of times to publish messages to the broker per routine.
	PubSub []int
	// contains filtered or unexported fields
}

ServiceTestConfig allows you to easily test a service configuration by running predefined tests against your custom service. You only need to provide a function to create the service, and how many of which test you want to run.

The default tests provided, all running with separate parallel routines are:

  • Sequential Call requests
  • Bi-directional streaming
  • Pub/Sub events brokering

You can provide an array of parallel routines to run for the request and stream tests. They will be run as matrix tests, so with each possible combination. Thus, in total (p * seq) + (p * streams) tests will be run.

func (*ServiceTestConfig) Handle

func (stc *ServiceTestConfig) Handle(ctx context.Context, msg *pb.HealthRequest) error

Handle is used as a test handler.

func (*ServiceTestConfig) HandleError

func (stc *ServiceTestConfig) HandleError(ctx context.Context, msg *pb.HealthRequest) error

HandleError is used as a test handler.

func (*ServiceTestConfig) Run

func (stc *ServiceTestConfig) Run(b *testing.B)

Run will start the benchmark tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL