testament

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

README

Testament

For all your testing needs.

Build Status

Testament

  1. Introduction
  2. Development

Introduction

These are common tools we usually use in our tests.

Function Description
AssertInError Check if an error is found in a deeply nested errors
RandomString Generates a randomly generated string by given length
GetFreeOpenPort Returns a port that is already claimed.
GetFreePort Returns a random open port.
RandomString Returns a randomly generates string with the length of count.
RandomLowerString Returns a randomly generates lower-cased string with a maximum length.
StringSlice Return a string slice with the provided length.
RandomStringSlice Return a random string slice with maximum length.
IntSlice Returns a slice of int elements with the provided length.
RandIntSlice Returns a slice of int elements with random length of less than n.
Int32Slice
RandInt32Slice
Int64Slice
RandInt64Slice
IntSliceComparer Is a go-cmp comparer that doesn't care if the slices are not sorted.
Int32SliceComparer
Int64SliceComparer
StringSliceComparer
RandomEmailAddress Returns a valid random email address.

Development

Prerequisite

This project supports Go >= 1.18. To run targets from the Makefile you need to install GNU make.

In order to install dependencies:

make dependencies

This also installs reflex to help with development process.

Running Tests

To watch for file changes and run unit_test:

make unit_test_watch
# or to run them with race flag:
make unit_test_race_watch

There is also a integration_test target for running integration tests.

Make Examples
make                           # shows help on targets.
make unit_test
make unit_test run=TestMyTest   # runs a specific test with regexp.
make unit_test dir=./path/...   # runs tests in a package.
make unit_test dir=./path/... run=TestSomethingElse

Please see the Makefile for more targets.

Documentation

Overview

Package testament provides convinience tools in testing.

Error Helper

The error specific functions are helpers for checking nested errors, or gRPC specific codes.

Network Helpers

These helpers can acquire random free network port.

Randomise Helpers

There are some useful helpers for creating random strings, slices, etc.

Comparer Helpers

These helpers are useful to be used with the google's cmp package.

Index

Constants

This section is empty.

Variables

View Source
var Int32SliceComparer = cmp.Comparer(func(a, b []int32) bool {
	return equal(a, b)
})

Int32SliceComparer is a go-cmp comparer that doesn't care if the slices in question is not sorted. The slices are copies and can be safely reused.

View Source
var Int64SliceComparer = cmp.Comparer(func(a, b []int64) bool {
	return equal(a, b)
})

Int64SliceComparer is a go-cmp comparer that doesn't care if the slices in question is not sorted. The slices are copies and can be safely reused.

View Source
var IntSliceComparer = cmp.Comparer(func(a, b []int) bool {
	return equal(a, b)
})

IntSliceComparer is a go-cmp comparer that doesn't care if the slices in question is not sorted. The slices are copies and can be safely reused.

View Source
var StringSliceComparer = cmp.Comparer(func(a, b []string) bool {
	return equal(a, b)
})

StringSliceComparer is a go-cmp comparer that doesn't care if the slices in question is not sorted. The slices are copies and can be safely reused.

View Source
var Uint32SliceComparer = cmp.Comparer(func(a, b []uint32) bool {
	return equal(a, b)
})

Uint32SliceComparer is a go-cmp comparer that doesn't care if the slices in question is not sorted. The slices are copies and can be safely reused.

View Source
var Uint64SliceComparer = cmp.Comparer(func(a, b []uint64) bool {
	return equal(a, b)
})

Uint64SliceComparer is a go-cmp comparer that doesn't care if the slices in question is not sorted. The slices are copies and can be safely reused.

Functions

func AssertInError

func AssertInError(t *testing.T, haystack, needle error) bool

AssertInError returns true if the needle is found in stack, which is created either with pkg/errors help, the multierror or Go's error wrap. It will fall back to checking the contents of the needle.Error() is in haystack.Error().

func AssertIsCode added in v0.1.0

func AssertIsCode(t *testing.T, err error, code codes.Code) bool

AssertIsCode is a helper to assert the err error contains the code.

func GetFreeOpenPort added in v0.1.0

func GetFreeOpenPort(t *testing.T) (uint, net.Listener)

GetFreeOpenPort returns a port that is already claimed. It closes the listener on test cleanup.

func GetFreePort added in v0.1.0

func GetFreePort(t *testing.T) uint

GetFreePort returns a random open port.

func Int32Slice added in v0.2.0

func Int32Slice(n int) []int32

Int32Slice returns a slice of int32 elements with provided length.

func Int64Slice added in v0.2.0

func Int64Slice(n int) []int64

Int64Slice returns a slice of int64 elements with provided length.

func IntSlice added in v0.2.0

func IntSlice(n int) []int

IntSlice returns a slice of int elements with the provided length.

func RandInt32Slice added in v0.2.0

func RandInt32Slice(n int) []int32

RandInt32Slice returns a slice of int32 elements with random length of less than n.

func RandInt64Slice added in v0.2.0

func RandInt64Slice(n int) []int64

RandInt64Slice returns a slice of int64 elements with random length of less than n.

func RandIntSlice added in v0.2.0

func RandIntSlice(n int) []int

RandIntSlice returns a slice of int elements with random length of less than n.

func RandUint32Slice added in v0.5.0

func RandUint32Slice(n int) []uint32

RandUint32Slice returns a slice of uint32 elements with random length of less than n.

func RandUint64Slice added in v0.5.0

func RandUint64Slice(n int) []uint64

RandUint64Slice returns a slice of uint64 elements with random length of less than n.

func RandUintSlice added in v0.5.0

func RandUintSlice(n int) []uint

RandUintSlice returns a slice of uint elements with random length of less than n.

func RandomEmailAddress added in v0.2.2

func RandomEmailAddress() string

RandomEmailAddress returns a random email address.

func RandomLowerString added in v0.2.0

func RandomLowerString(count int) string

RandomLowerString returns a randomly generates lower-cased string with the length of count.

func RandomS3Filename added in v0.4.0

func RandomS3Filename() string

RandomS3Filename returns a random S3 filename with a subfolder.

func RandomString added in v0.1.0

func RandomString(count int) string

RandomString returns a randomly generates string with the length of count.

func RandomStringSlice added in v0.2.0

func RandomStringSlice(to int) []string

RandomStringSlice return a random string slice with maximum length of to.

func StringSlice added in v0.2.0

func StringSlice(n int) []string

StringSlice return a string slice with the provided length.

func Uint32Slice added in v0.5.0

func Uint32Slice(n int) []uint32

Uint32Slice returns a slice of uint32 elements with provided length.

func Uint64Slice added in v0.5.0

func Uint64Slice(n int) []uint64

Uint64Slice returns a slice of uint64 elements with provided length.

func UintSlice added in v0.5.0

func UintSlice(n int) []uint

UintSlice returns a slice of uint elements with the provided length.

Types

This section is empty.

Jump to

Keyboard shortcuts

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