Categorygithub.com/programmfabrik/go-test-utils
modulepackage
0.0.0-20191114143449-b8e16b04adb1
Repository: https://github.com/programmfabrik/go-test-utils.git
Documentation: pkg.go.dev

# 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 ;))

# Functions

AsserErrorEqualsAny checks if the actually error is equal to one in your error slice.
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.
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.
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.
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.
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.
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.
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.
AssertStringContainsSubstringsInOrder checks if the given string contains all the expected strings in the right order.
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.
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.
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.
ClearSlash removes double forward and backward slashes from your string "//" => "/" && "\\" => "\" Input: in string The string to clean Output: string The cleaned up string.
ExpectError checks if the given error is != nil.
func checks if the given error is == nil.
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).
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.

# Type aliases

HandleFunc the type of func that a typical go http handler has.
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.