datadriven

package
v0.0.0-...-417737b Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunTest

func RunTest(t *testing.T, path string, f func(d *TestData) string)

RunTest invokes a data-driven test. The test cases are contained in a separate test file and are dynamically loaded, parsed, and executed by this testing framework. By convention, test files are typically located in a sub-directory called "testdata". Each test file has the following format:

<command>[,<command>...] [arg | arg=val | arg=(val1, val2, ...)]...
<input to the command>
----
<expected results>

The command input can contain blank lines. However, by default, the expected results cannot contain blank lines. This alternate syntax allows the use of blank lines:

<command>[,<command>...] [arg | arg=val | arg=(val1, val2, ...)]...
<input to the command>
----
----
<expected results>

<more expected results>
----
----

To execute data-driven tests, pass the path of the test file as well as a function which can interpret and execute whatever commands are present in the test file. The framework invokes the function, passing it information about the test case in a TestData struct. The function then returns the actual results of the case, which this function compares with the expected results, and either succeeds or fails the test.

func RunTestFromString

func RunTestFromString(t *testing.T, input string, f func(d *TestData) string)

RunTestFromString is a version of RunTest which takes the contents of a test directly.

func Walk

func Walk(t *testing.T, path string, f func(t *testing.T, path string))

Walk goes through all the files in a subdirectory, creating subtests to match the file hierarchy; for each "leaf" file, the given function is called.

This can be used in conjunction with RunTest. For example:

 datadriven.Walk(t, path, func (t *testing.T, path string) {
   // initialize per-test state
   datadriven.RunTest(t, path, func (d *datadriven.TestData) {
    // ...
   }
 }

Files:
  testdata/typing
  testdata/logprops/scan
  testdata/logprops/select

If path is "testdata/typing", the function is called once and no subtests
care created.

If path is "testdata/logprops", the function is called two times, in
separate subtests /scan, /select.

If path is "testdata", the function is called three times, in subtest
hierarchy /typing, /logprops/scan, /logprops/select.

Types

type CmdArg

type CmdArg struct {
	Key  string
	Vals []string
}

CmdArg contains information about an argument on the directive line. An argument is specified in one of the following forms:

  • argument
  • argument=value
  • argument=(values, ...)

func (CmdArg) String

func (arg CmdArg) String() string

type TestData

type TestData struct {
	Pos string // reader and line number

	// Cmd is the first string on the directive line (up to the first whitespace).
	Cmd string

	CmdArgs []CmdArg

	Input    string
	Expected string
}

TestData contains information about one data-driven test case that was parsed from the test file.

func (TestData) Fatalf

func (td TestData) Fatalf(tb testing.TB, format string, args ...interface{})

Fatalf wraps a fatal testing error with test file position information, so that it's easy to locate the source of the error.

func (*TestData) HasArg

func (td *TestData) HasArg(key string) bool

HasArg determines if `key` appears in CmdArgs.

func (*TestData) ScanArgs

func (td *TestData) ScanArgs(t *testing.T, key string, dests ...interface{})

ScanArgs looks up the first CmdArg matching the given key and scans it into the given destinations in order. If the arg does not exist, the number of destinations does not match that of the arguments, or a destination can not be populated from its matching value, a fatal error results.

For example, for a TestData originating from

cmd arg1=50 arg2=yoruba arg3=(50, 50, 50)

the following would be valid:

var i1, i2, i3, i4 int var s string td.ScanArgs(t, "arg1", &i1) td.ScanArgs(t, "arg2", &s) td.ScanArgs(t, "arg3", &i2, &i3, &i4)

Jump to

Keyboard shortcuts

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