tester: github.com/workfit/tester/ddt Index | Examples | Files

package ddt

import "github.com/workfit/tester/ddt"

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



Package Files


func LoadTestCasesFromDerivedJSONFile Uses

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.


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)


Actual Answer: 42
Actual Answer: 42

Package ddt imports 5 packages (graph). Updated 2019-04-21. Refresh now. Tools for package owners.