go_test_utils

package module
v0.0.0-...-b8e16b0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: MIT Imports: 6 Imported by: 1

README

GoDoc

Go Test Utils

This small packages gives you some handy testing functions, to supercharge your go unit testing workflow.

Full documentation? Use the source, Luke (or go to GoDoc)

Do you have your own small functions, that help you with you unit test? Do not hesitate to open a pull request so we can build a great tool chain and make unit go testing even more effective.

Error

ExpectError(t *testing.T,err error, msg string) Checks if the given error is an error

ExpectNoError(t *testing.T,err error, msg string) Checks if the given error is nil

Assert

AssertStringEquals(t testing.TB, expected, got string) checks if two strings are equal

AssertIntEquals(t testing.TB, expected, got int) checks if two strings are equal

AssertIsError(t testing.TB, err error) checks if there is an error

AssertErrorEquals(t *testing.T, expected, got error) checks if the actually error is equal the expected one, by doing a string compare

AsserErrorEqualsAny(t *testing.T, got error, expectAnyIn []error) checks if the actually error is equal to one in your error slice.

AssertErrorContains(t *testing.T, err error, shouldContain string) checks if the given error contains the expected substring

AssertStringContainsSubstringsInOrder(t *testing.T, body string, expectedStrings []string) checks if the given string contains all the expected strings in the right order.

AssertStringContainsSubstringsNoOrder(t *testing.T, body string, expectedStrings []string) checks if the given string contains all the expected strings, we do not care about the order

AssertStringContainsNoneOfTheSubstrings(t *testing.T, body string, nonExpectedStrings []string) checks if the given string contains any of the nonExpectedStrings

AssertMapsEqual(t *testing.T, got, expected map[string]interface{}) checks if two maps have the exact same content

AssertStringArraysEqualNoOrder(t *testing.T, got, expected []string) checks if two string slices are the same, but do not have the same order

Server

type HandleFunc func(*http.ResponseWriter, *http.Request) the type of func that a typical go http handler has

type Routes map[string]HandleFunc is a map of functions, that help you to define your testserver

NewTestServer(routes Routes) *httptest.Server creates a new go testing server with the given routes, so you can define more complex test server setups in no time

Util

ClearSlash(in string) string removes double forward and backward slashes from your string

CheckFor500(t *testing.T, statusCode int) checks if the given http status code equals StatusInternalServerError

JsonEqual(s1, s2 string) bool is an easy json compare function that tries to tell you if the given json strings are equal or not


We use this toolset internally in our backend team @Programmfabrik (FYI: We are hiring ;))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsserErrorEqualsAny

func AsserErrorEqualsAny(t *testing.T, got error, expectAnyIn []error)

AsserErrorEqualsAny checks if the actually error is equal to one in your error slice. You can use this function if you expect an error, but you do not exactly know which one Input:

t *testing.T 		The testing object, so we can call the return functions on it
got error	 		The error you actually got
expectAnyIn []error	All the errors that are a valid result

func AssertErrorContains

func AssertErrorContains(t *testing.T, err error, shouldContain string)

AssertErrorContains checks if the given error contains the expected substring Input:

t *testing.T 			The testing object, so we can call the return functions on it
err error	 			The error you actually got
shouldContain string 	The substring you expect in the error

func AssertErrorEquals

func AssertErrorEquals(t *testing.T, expected, got error)

AssertErrorEquals checks if the actually error is equal the expected one, by doing a string compare Input:

t *testing.T 		The testing object, so we can call the return functions on it
expected error	 	The error you expect
got error	 		The error you actually got

func AssertIntEquals

func AssertIntEquals(t testing.TB, expected, got int)
	AssertIntEquals checks if two strings are equal
	Input:
		t *testing.T 		The testing object, so we can call the return functions on it
  	expected int 		The int you want to have
  	got int				The int you actually got from your test result

func AssertIsError

func AssertIsError(t testing.TB, err error)

AssertIsError checks if there is an error Input:

t *testing.T 		The testing object, so we can call the return functions on it
err error	 		The error you want to check for

func AssertMapsEqual

func AssertMapsEqual(t *testing.T, got, expected map[string]interface{})

AssertMapsEqual checks if two maps have the exact same content If a string is found, the test fails and we print the complete body Prints the complete body if wrong order is present Input:

t *testing.T 					The testing object, so we can call the return functions on it
got map[string]interface{}	 	The map you got
expected map[string]interface{}	The map you expect to have

func AssertStringArraysEqualNoOrder

func AssertStringArraysEqualNoOrder(t *testing.T, got, expected []string)

AssertStringArraysEqualNoOrder checks if two string slices are the same, but do not have the same order Input:

t *testing.T 		The testing object, so we can call the return functions on it
got []string	 	The slice you got
expected []string	The map you expect to have

func AssertStringContainsNoneOfTheSubstrings

func AssertStringContainsNoneOfTheSubstrings(t *testing.T, body string, nonExpectedStrings []string)

AssertStringContainsNoneOfTheSubstrings checks if the given string contains any of the nonExpectedStrings If a string is found, the test fails and we print the complete body Prints the complete body if wrong order is present Input:

t *testing.T 				The testing object, so we can call the return functions on it
body string	 				The string you got
nonExpectedStrings string 	The substrings you expect not to be in the body string

func AssertStringContainsSubstringsInOrder

func AssertStringContainsSubstringsInOrder(t *testing.T, body string, expectedStrings []string)

AssertStringContainsSubstringsInOrder checks if the given string contains all the expected strings in the right order. Prints the complete body if wrong order is present Input:

t *testing.T 			The testing object, so we can call the return functions on it
body string	 			The string you got
expectedStrings string 	The substrings you expect to be in order in the body string

func AssertStringContainsSubstringsNoOrder

func AssertStringContainsSubstringsNoOrder(t *testing.T, body string, expectedStrings []string)

AssertStringContainsSubstringsNoOrder checks if the given string contains all the expected strings, we do not care about the order Prints the complete body if wrong order is present Input:

t *testing.T 			The testing object, so we can call the return functions on it
body string	 			The string you got
expectedStrings string 	The substrings you expect to be in the body string

func AssertStringEquals

func AssertStringEquals(t testing.TB, expected, got string)
	AssertStringEquals checks if two strings are equal
	Input:
		t *testing.T 		The testing object, so we can call the return functions on it
  	expected string 	Te string you want to have
  	got string			The string you actually got from your test result

func CheckFor500

func CheckFor500(t *testing.T, statusCode int)

CheckFor500 checks if the given http status code equals StatusInternalServerError Input:

t *testing.T 		The testing object, so we can call the return functions on it
statusCode int		The http status code you want to check

func ClearSlash

func ClearSlash(in string) string

ClearSlash removes double forward and backward slashes from your string "//" => "/" && "\\" => "\" Input:

in string	The string to clean

Output:

string		The cleaned up string

func ExpectError

func ExpectError(t *testing.T, err error, msg string)

ExpectError checks if the given error is != nil. If it is the msg is written to testing.Error

func ExpectNoError

func ExpectNoError(t *testing.T, err error, msg string)

func checks if the given error is == nil. If it is not the msg is written to the testing.Error

func JsonEqual

func JsonEqual(s1, s2 string) bool

JsonEqual is an easy json compare function that tries to tell you if the given json strings are equal or not Input:

s1, s2 string	The two json strings you want to compare

Output:

bool			Bool that indicates if the strings are equal jsons (true if they are equal)

func NewTestServer

func NewTestServer(routes Routes) *httptest.Server

NewTestServer creates a new go testing server with the given routes, so you can define more complex test server setups in no time Input

routes Routes	The map of routes, that define your test server

Types

type HandleFunc

type HandleFunc func(*http.ResponseWriter, *http.Request)

HandleFunc the type of func that a typical go http handler has

type Routes

type Routes map[string]HandleFunc

Routes is a map of functions, that help you to define your testserver Values:

key string		The path of your route (e.g "/test/my/cool/func")
func HandleFunc	The go http handler that will be served at that route

Jump to

Keyboard shortcuts

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