ddt

package
Version: v0.0.0-...-6b51fb8 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2019 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package ddt provides utilities to populate test cases for data-driven tests.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadTestCasesFromDerivedJSONFile

func LoadTestCasesFromDerivedJSONFile(testCasesToLoad interface{}) error

LoadTestCasesFromDerivedJSONFile loads test cases from a JSON file whose path is derived from the caller's test function name and file. The file path is "<package under test>/_ddt/<basename of test file>.json"; for example, "hitchhiker/_ddt/question_test.json" with the following schema:

{"testCases": [{<properties of the test case to unmarshal>}, ...]}

For example, the JSON content may look like the following:

{
  "testCases": [
    {
      "id": "The Ultimate Question",
      "input": {
        "question": "What do you get when you multiply six by nine?",
        "timeoutInHours": 65700000000,
        "config": {"base": 13}
      },
      "expected": {
        "answer": "42",
        "error": null
      }
    }
  ]
}

The details of the test case struct are left for the tester to specify.

Example
t := &testing.T{}
answer := func(q string, timeout int, config map[string]interface{}) (string, error) { return "42", nil }
var testCases []struct {
	ID    string `json:"id"`
	Input struct {
		Question       string                 `json:"question"`
		TimeoutInHours int                    `json:"timeoutInHours"`
		Config         map[string]interface{} `json:"config"`
	} `json:"input"`
	Expected struct {
		Answer string `json:"answer"`
		Error  error  `json:"error"`
	} `json:"expected"`
}

mustWriteJSONFile("ExampleLoadTestCasesFromDerivedJSONFile.json", vanillaContent)
err := ddt.LoadTestCasesFromDerivedJSONFile(&testCases)
if assert.For(t).ThatActualError(err).IsNil().Passed() {
	for _, c := range testCases {
		a, err := answer(c.Input.Question, c.Input.TimeoutInHours, c.Input.Config)
		assert.For(t, c.ID).ThatActual(a).Equals(c.Expected.Answer)
		assert.For(t, c.ID).ThatActualError(err).Equals(c.Expected.Error)
		fmt.Println("Actual Answer:", a)
	}
}
Output:

Actual Answer: 42
Actual Answer: 42

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

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