gotestutil

package module
v0.0.0-...-1a02057 Latest Latest
Warning

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

Go to latest
Published: May 26, 2017 License: BSD-3-Clause Imports: 6 Imported by: 0

README

go-testutil

GO Assertion methods for testing

The testutil package provides an assertion framework for unit testing. The package assumes these are executed in the context of gotest, and the first parameter of each method is the *testing.T parameter of the test method. If any assertion fails, the assertion calls t.Fatalf(), logs the message, and terminates the test method.

Usage examples:

func TestAssertTrue(t *testing.T)
	AssertTrue(t, true, "Expected true")
}

func TestAssertFalse(t *testing.T) { AssertFalse(t, false, "Expected false.") }

func TestAssertNil(t *testing.T) { var b []int AssertNil(t, b, "Expected nil.") }

func TestAssertNotNil(t *testing.T) { var b []int = make([]int, 2) AssertNotNil(t, b, "Expected nil.") }

func TestAssertEmptyString(t *testing.T) { AssertEmptyString(t, "", "Expected empty string.") }

func TestAssertNotEmptyString(t *testing.T) { AssertNotEmptyString(t, "test", "Expected non-empty string.") }

func TestAssertStringsEqual(t *testing.T) { AssertStringsEqual(t, "test", "test", "Strings did not match.") }

func TestAssertStringsNotEqualEqual(t *testing.T) { AssertStringsNotEqual(t, "test", "test1", "Strings matched.") }

func TestAssertEqual(t *testing.T) { AssertEqual(t, 1, 1, "Integers did not match.") AssertEqual(t, 1.0, 1.0, "Floats did not match.") }

func TestAssertGreaterThan(t *testing.T) { AssertGreaterThan(t, 2, 1, "Integer not greater-than for values %d > %d.", 2, 1) AssertGreaterThan(t, 2.0, 0.0, "Float not greater-than") }

func TestAssertGreaterThanOrEqual(t *testing.T) { AssertGreaterThanOrEqual(t, 2, 1, "Integer not greater-than.") AssertGreaterThanOrEqual(t, 2, 2, "Integer not equal.") AssertGreaterThanOrEqual(t, 2.0, 1.0, "Float not greater-than.") AssertGreaterThanOrEqual(t, 2.0, 2.0, "Float not equal") }

func TestAssertLessThan(t *testing.T) { AssertLessThan(t, -1, 1, "Integer not less-than.") AssertLessThan(t, 1, 2, "Integer not less-than.") AssertLessThan(t, 1.0, 2.0, "Float not less-than.") }

func TestAssertLessThanOrEqual(t *testing.T) { AssertLessThanOrEqual(t, 1, 2, "Integer not less-than.") AssertLessThanOrEqual(t, 2, 2, "Integer not equal.") AssertLessThanOrEqual(t, 1.0, 2.0, "Float not less-than.") AssertLessThanOrEqual(t, 2.0, 2.0, "Float not equal") }

func trueFalseTest(v interface{}) bool { return v.(bool) }

func TestAssertTrueFunc(t *testing.T) { AssertTrueFunc(t, true, trueFalseTest, "Did not return true.") }

func TestAssertFalseFunc(t *testing.T) { AssertFalseFunc(t, false, trueFalseTest, "Did not return false.") }

Documentation

Overview

The testutil package provides an assertion framework for unit testing. The package assumes these are executed in the context of gotest, and the first parameter of each method is the *testing.T parameter of the test method. If any assertion fails, the assertion calls t.Fatalf(), logs the message, and terminates the test method.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEmptyString

func AssertEmptyString(t *testing.T, v string, format string, a ...interface{})

Assert a string value is equal to the empty string (""). The method takes the t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertEqual

func AssertEqual(t *testing.T, v1 interface{}, v2 interface{}, format string, a ...interface{})

Assert that two values are equal. The method takes the t parameter from the test method, the values to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method. Assumes each of v1 and v2 are typed values (not constants)

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertFalse

func AssertFalse(t *testing.T, b bool, format string, a ...interface{})

Assert a value evaluates to false The method takes the *t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertFalseFunc

func AssertFalseFunc(t *testing.T, v interface{}, f func(x interface{}) bool, format string, a ...interface{})

Assert the a function that accepts a parameter (v) returns false. The method takes the t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertGreaterThan

func AssertGreaterThan(t *testing.T, v1 interface{}, v2 interface{}, format string, a ...interface{})

Assert the first value (v1) is numerically greater than the second value (v2). The method takes the t parameter from the test method, the values to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertGreaterThanOrEqual

func AssertGreaterThanOrEqual(t *testing.T, v1 interface{}, v2 interface{}, format string, a ...interface{})

Assert the first value (v1) is greater-than-or-equal-to the second value (v2) The method takes the t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertLessThan

func AssertLessThan(t *testing.T, v1 interface{}, v2 interface{}, format string, a ...interface{})

Assert the first value (v1) is less-than the second value (v2) The method takes the t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertLessThanOrEqual

func AssertLessThanOrEqual(t *testing.T, v1 interface{}, v2 interface{}, format string, a ...interface{})

Assert the first value (v1) is less-than-or-equal-to the second value (v2) The method takes the *t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertNil

func AssertNil(t *testing.T, v interface{}, format string, a ...interface{})

Assert a value is nil. The method takes the t paramteter from the test method, the value to be asserted as nil, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertNotEmptyString

func AssertNotEmptyString(t *testing.T, v string, format string, a ...interface{})

Assert a string value is NOT equal to the empty string. The method takes the *t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertNotNil

func AssertNotNil(t *testing.T, v interface{}, format string, a ...interface{})

Assert a value is NOT nil. The method takes the t paramteter from the test method, the value to be asserted as NOT nil, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertStringsEqual

func AssertStringsEqual(t *testing.T, v1 string, v2 string, format string, a ...interface{})

Assert two strings are equivalent, i.e. have the string characters/runes. The method takes the *t parameter from the test method, the two strings to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertStringsNotEqual

func AssertStringsNotEqual(t *testing.T, v1 string, v2 string, format string, a ...interface{})

Assert two strinsg are NOT equivalent, i.e. the string values are not the same. The method takes the *t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertTextInFiles

func AssertTextInFiles(t *testing.T, fileMap map[int]string, needle string) (found bool)

Assert a given string is found in a list of files provided as a map[int]string. The method takes the t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertTextNotInFiles

func AssertTextNotInFiles(t *testing.T, fileMap map[int]string, needle string) (found bool)

Assert a given string is NOT found in a list of files provided as a map[int]string. The method takes the t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertTrue

func AssertTrue(t *testing.T, b bool, format string, a ...interface{})

Assert a value evaluates to true The method takes the t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

func AssertTrueFunc

func AssertTrueFunc(t *testing.T, v interface{}, f func(x interface{}) bool, format string, a ...interface{})

Assert the a function that accepts a parameter (v) returns true. The method takes the t parameter from the test method, the value to be asserted, and a message printed if the assertion fails. The format and the a variadic parameters conform the the fmt.Fatalf() method.

If the assertion fails, t.Fatalf() is called terminating the test script.

Types

This section is empty.

Jump to

Keyboard shortcuts

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