Documentation ¶
Overview ¶
Package testutils provides a framework and helper functions for use during unit testing.
Index ¶
- func BoolPtr(b bool) *bool
- func CallMethod(t *testing.T, obj interface{}, methodName string, params []interface{}) []interface{}
- func CastToMapStringString(t *testing.T, data interface{}) map[string]string
- func CastToStringList(t *testing.T, data interface{}) []string
- func CheckError(u TestUtil) bool
- func CheckFieldGetter(u TestUtil, fieldName string, fieldInfo FieldInfo) bool
- func CheckFieldValue(u TestUtil, fieldName string, fieldInfo FieldInfo) bool
- func CheckFieldsGetter(u TestUtil) bool
- func CheckFieldsValue(u TestUtil) bool
- func CheckNotNil(u TestUtil, name string, a, e interface{}) bool
- func CheckObjStatusFunc(u TestUtil) bool
- func CompareAsJSON(t *testing.T, one, two interface{}) bool
- func CompareJSON(u TestUtil, name string, actual, expected interface{}) bool
- func CompareReflectDeepEqual(u TestUtil, name string, actual, expected interface{}) bool
- func ContainsStringArray(one, two []string, first bool) bool
- func ContainsStrings(expected []string, result string) bool
- func DefaultCheckFunc(u TestUtil) bool
- func DefaultPrepFunc(u TestUtil)
- func DisplayStrings(strs []string) string
- func GetPointerValue(i interface{}) interface{}
- func HandlePanic(t *testing.T)
- func IsPointer(i interface{}) bool
- func MapKeysToList(envsMap map[string]string) []string
- func ReadBuf(out io.Reader) *[]string
- func ReportCallJSON(u TestUtil)
- func ReportCallSpew(u TestUtil)
- func ReportJSON(u TestUtil, name string, actual, expected interface{})
- func ReportSpew(u TestUtil, name string, actual, expected interface{})
- func SetEnv(t *testing.T, envName, envValue string)
- func SetEnvs(t *testing.T, envSettings map[string]string)
- func ToJSON(t *testing.T, data interface{}) string
- func UnsetEnv(t *testing.T, envName string)
- func UnsetEnvs(t *testing.T, names []string)
- type CallMethodFunc
- type CheckTestI
- type ComparerI
- type DefTest
- type FieldInfo
- type Fields
- type GetFieldFunc
- type ObjectStatus
- type PrepTestI
- type ReportDiffI
- type SetFieldFunc
- type TestUtil
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallMethod ¶
func CallMethod(t *testing.T, obj interface{}, methodName string, params []interface{}) []interface{}
CallMethod calls a method on an object and returns the results.
func CastToMapStringString ¶
CastToMapStringString casts an interface to a map[string]string.
func CastToStringList ¶
CastToStringList casts an interface to a []string.
func CheckError ¶
CheckError implements the CheckTestI interface, verifying that a error contains expected text strings. It then calls the default check function to verify other results, if any.
func CheckFieldGetter ¶
CheckFieldGetter will the getter function of a field and check the value is equal to the expected value and set the test to failed if not.
func CheckFieldValue ¶
CheckFieldValue will check if a field value is equal to the expected value and set the test to failed if not.
func CheckFieldsGetter ¶
CheckFieldsGetter will call the getter functions on an object to check the expected values are equal to the actual value and set the test to failed if not.
func CheckFieldsValue ¶
CheckFieldsValue will get the value of object fields and check the expected values are equal to the actual value and set the test to failed if not.
func CheckNotNil ¶ added in v0.0.39
CheckNotNil is passed two interfaces and checks if both or neither are nil. If both are nil it returns true, if neither are nil it returns false. If one is nil and the other is not it reports the difference and returns nil.
func CheckObjStatusFunc ¶
CheckObjStatusFunc checks object fields values against expected and report if different.
func CompareAsJSON ¶
CompareAsJSON compares two interfaces as JSON.
func CompareJSON ¶ added in v0.0.42
CompareJSON compares values by converting to json.
func CompareReflectDeepEqual ¶ added in v0.0.39
CompareReflectDeepEqual compares values using reflect.DeepEqual().
func ContainsStringArray ¶
ContainsStringArray checks items in two string arrays verifying that the second string array is contained in the first.
func ContainsStrings ¶
ContainsStrings checks that all elements in a list of strings are contained in another string.
func DefaultCheckFunc ¶
DefaultCheckFunc is the default check test function that compares actual and expected.
func DefaultPrepFunc ¶
func DefaultPrepFunc(u TestUtil)
DefaultPrepFunc is the default pre test function that prints the test number and name.
func DisplayStrings ¶
DisplayStrings returns a []string as a single string, one element per line.
func GetPointerValue ¶
func GetPointerValue(i interface{}) interface{}
GetPointerValue returns what a pointer points to.
func HandlePanic ¶
HandlePanic handles a panic in test code calling testing.T.Fatal() with interface returned by recover().
func IsPointer ¶
func IsPointer(i interface{}) bool
IsPointer returns true if a variable is a pointer.
func MapKeysToList ¶
MapKeysToList returns the keys of a map[string]string as a list.
func ReportCallJSON ¶ added in v0.0.39
func ReportCallJSON(u TestUtil)
ReportCallJSON uses json to report on result of a function or method call. It displays the input, result and expected result as well as the differences between the actual and expected results.
func ReportCallSpew ¶ added in v0.0.39
func ReportCallSpew(u TestUtil)
ReportCallSpew uses spew to report on result of a function or method call. It displays the input, result and expected result as well as the differences between the actual and expected results.
func ReportJSON ¶
ReportJSON uses json to report the differences between two values. It displays the actual and expected values as well as the differences between them.
func ReportSpew ¶
ReportSpew uses spew to report the differences between two values. It displays the actual and expected values as well as the differences between them.
Types ¶
type CallMethodFunc ¶
type CallMethodFunc func(t *testing.T, obj interface{}, methodName string, values []interface{}) []interface{}
CallMethodFunc is the function to call a method on an object.
type CheckTestI ¶
CheckTestI defines function to be called after test to check result.
type DefTest ¶
type DefTest struct { Number int // Test number. Description string // Test description. EnvVars []string // List of environmental variable to be reset at the start of each test Config interface{} // Test configuration information to be used by test function or custom pre/post test functions. Inputs []interface{} // Test inputs. Expected []interface{} // Test expected results. Results []interface{} // Test results. ObjStatus *ObjectStatus // Details of object under test including field names and expected values, used by CheckFunc to verify values. // PrepFunc is function to be called before a test, leave unset to call default - which prints the test number and name. PrepFunc PrepTestI // CheckFunc is function to be called to check a test results, leave unset to call default. // Default compares actual results with expected results and verifies object status. CheckFunc CheckTestI // ResultsCompareFunc is function to be called to compare a test results, leave unset to call default. // Default compares actual results with expected results using reflect.DeepEqual(). ResultsCompareFunc ComparerI // ResultsReportFunc is function to be called to report difference in test results, leave unset to call default - which uses spew.Sdump(). ResultsReportFunc ReportDiffI // FieldCompareFunc is function to be called to compare a field values, leave unset to call default. // Default compares actual results with expected results using reflect.DeepEqual(). FieldCompareFunc ComparerI // FieldCompareFunc is function to be called to report difference in field values, leave unset to call default - which uses spew.Sdump(). FieldReportFunc ReportDiffI }
DefTest generic test data structure.
type FieldInfo ¶
type FieldInfo struct { Reporter ReportDiffI `json:"reporter,omitempty"` // Function to do field specific reporting of differences, nil if not set. Comparer ComparerI `json:"comparer,omitempty"` // Function to do field specific compare, nil if not set. GetterMethod string `json:"getter,omitempty"` // The method to get the value, nil if no getter method. SetterMethod string `json:"setter,omitempty"` // The method to get the value, nil if no setter method. FieldValue interface{} `json:"value"` // The value to set or expected value to verify. }
FieldInfo holds information about a field of a struct.
type GetFieldFunc ¶
GetFieldFunc is the function to call to get the value of a field of an object.
type ObjectStatus ¶
type ObjectStatus struct { Object interface{} // The object or interface under test, this needs to be set during test before calling post test actions. GetField GetFieldFunc // The function to call to get a field value. SetField SetFieldFunc // The function to call to set a field value. CallMethod CallMethodFunc // The function to call a method on an object. Fields Fields // The fields of an object. }
ObjectStatus hold details of the object under test. This can be used to verify the internal state of an object after a test.
func CopyObjectStatus ¶
func CopyObjectStatus(objStatus *ObjectStatus) *ObjectStatus
CopyObjectStatus copys an ObjectStatus object leaving Fields element empty.
func SetObjStatusFields ¶
func SetObjStatusFields(t *testing.T, objStatus *ObjectStatus, fieldValues map[string]interface{}, retain bool) *ObjectStatus
SetObjStatusFields sets the values in the FieldInfo struct, used to update a template ObjectStatus with values for a given test.
type PrepTestI ¶
type PrepTestI func(u TestUtil)
PrepTestI defines function to be called before running a test.
type ReportDiffI ¶ added in v0.0.39
ReportDiffI defines the report difference function interface.
type SetFieldFunc ¶
SetFieldFunc is the function to call to set the value of a field of an object.
type TestUtil ¶
type TestUtil interface { CallPrepFunc() // Call the custom or default test preparation function. CallCheckFunc() bool // Call the custom or default test checking function. Testing() *testing.T // testing object. SetFailTests(value bool) // Set the fail test setting to verify test check reporting. FailTests() bool // Get the fail test setting. SetVerbose(value bool) // Set the verbose setting. Verbose() bool // Get the verbose setting. SetTestData(testData *DefTest) // Set the test data. TestData() *DefTest // Get the test data. // ResultsComparer calls the specified comparer, default checking function calls this to call test data's CompareFunc or CompareReflectDeepEqual if not set. ResultsComparer() bool // FieldComparer calls the field comparer, default checking function calls this to call test data's CompareFunc or CompareReflectDeepEqual if not set. FieldComparer(name string, actual, expected interface{}) bool // ResultReporter calls the specified reporter, default checking function calls this to call test data's ResultsReportFunc or ReportSpew if not set. ResultsReporter() // FieldReporter calls the specified reporter, default checking function calls this to call test data's ReportFieldsFunc or ReportSpew if not set. FieldReporter(name string, actual, expected interface{}) }
TestUtil the interface used to provide testing utilities.