tbltest

package module
v0.0.0-...-1865222 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2018 License: MIT Imports: 10 Imported by: 0

README

With the upcoming release of 1.10 being release, and 1.7 introducing subtests, I think it is better to no longer use this package and instead do the following:

package test

import "testing"

func TestFoo(t *testing.T) {
	/*

		With this style of writing test we break our tests into three sections.

		The first defines the test case; which takes the inputs and the expected outputs and wraps them
		up into a single unit.

		I like to match the names of the function parameters to the field names. Say you are testing a
		function that take two parameters a, and b and returns a^b.
		```go
			func raise(a, b int) float {
				...
			}
		```
		The test case should look like:
	*/
	type testCase struct {
		a int // The a parameter to raise
		b int // the b parameter to raise

		expected float // a^b
	}
	/*

		The next section is the test func, which we will call fn. (This will be a curried function.)
		This function should take a test case, and return a function that will actually run the test.
		For the most part you can ignore this, and just copy the template below; filling the the necessary
		parts.

		By having the test defined right after the test case, it is easy to see what the test is doing,
		and what each field in the test case means.

		So, to test our fiticious raise function we would do the following:
	*/
	fn := func(tc testCase) func(t *testing.T) {
		return func(t *testing.T) {

			/* Here is where we would add our test code, replace as needed for your own functions. */
			got := raise(tc.a, tc.b)
			if got != tc.expected {
				t.Errorf("invalid answer from raise, expected %v got %v", tc.expected, got)
			}

			/* rest of the function follows */
		}
	}

	/*
		Next we define our actual test cases. Each test case has a unique name, which is the key
		in the map, followed by the values for the test case.

		By using a map, we get two benificates:
		1. Each test will have a unique name, that we can use to run just that test.
		2. The test are run an a random order everytime insuring that there aren't an
			dependencies between test runs.

	*/
	tests := map[string]testCase{
		"2^5": testCase{
			a:        2,
			b:        5,
			expected: 32,
		},
		"5^2": testcase{
			a:        5,
			b:        2,
			expected: 25,
		},
	}

	/*
		The last section is the boilerplate that actually runs the tests.
	*/
	for name, tc := range tests {
		t.Run(name, fn(tc))
	}
}

In Play In order only run a pictular test use: -run "TestFoo/foo"

tbltest

GoDoc Build Status Go Report Card

This is a simple package to help with table driven tests. Given a set of test cases, it will call the provided function each test case.

This helps remove boiler plate that comes with writing table driven code.

Example

package main

import (
  "github.com/gdey/tbltest"
  "testing"
)

func Foo(foo string) bool {
  if foo == "foo" {
    return true
  }
  return false
}

// tlb version
func TestFoo(t *testing.T) {
  type testcase struct {
    foo      string
    expected bool
  }

  tests := tlbtest.Cases(
    testcase{
      foo:      "foo",
      expected: true,
    },
    testcase{
      foo:      "bar",
      expected: false,
    },
  )

  tests.Run(func(tc testcase) {
    if Foo(tc.foo) != tc.expected {
      t.FailNow()
    }
  })

}

// standard version
func TestFoo(t *testing.T) {
  type testCase struct {
    foo      string
    expected bool
  }

  tests := []testCase{
    testCase{
      foo:      "foo",
      expected: true,
    },
    testcase{
      foo:      "bar",
      expected: false,
    },
  }

  for _, tc := range tests {
    if Foo(tc.foo) != tc.expected {
      t.FailNow()
    }
  }
}

command line flags

In addition, the tool adds a new command line flag to help with debugging.

--tblTest.RunOrder : Allows one to specify the testcases's and the order they should run in. This is usually helpful, when you are trying to fix one failing test, that you want to keep running over and over again.

Why

The biggest benefits provided by this library are:

  1. By default randomizes the order in which the test cases are tested. Ensuring that there are no hidden dependencies between the tests caused by to call order.
  2. Make it easy to run a single test case using the --tblTest.runOrder command line parameter.

Documentation

Overview

Package tbltest implements helper functions to help write table driven tests.

The most common way to use this package is in a test function.

 func TestFoo(t testing.T){
    type testcase struct {
        first    string
        last     string
        expected string
    }
    test := tbltest.Cases(
        testcase{
            first    : "Gautam",
            last     : "Dey",
            expected : "Gautam Dey"
        },
    )
    test.Run(func(tc testcase){
        if tc.expected != join(tc.first," ",tc.last) {
            t.Errorf("Failed test.")
        }
    })
 }

The function provided to run is called for each testcase in the cases.
If the function provided returns a boolean, the return value will be used to
determine weather or not to continue onto the next testcase.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MyCallerFileLine

func MyCallerFileLine() string

MyCallerFileLine returns the FileLine of the caller of the function that called it :)

Types

type Test

type Test struct {

	// InOrder defines weather to run the test case in the order defined or randomly.
	// This option is overridden by the tblTest.RunOrder command line flag.
	InOrder bool

	// The order in which to run these tests. This will be overridden by the Command line flag.
	RunOrder string
	// contains filtered or unexported fields
}

Test holds the testcases.

func Cases

func Cases(testcases ...TestCase) *Test

Cases takes a list of test cases to use for the table driven tests.

The test cases can be any type, as long as they are all the same.

func (*Test) AddCases

func (tc *Test) AddCases(testcases ...TestCase)

AddCases takes a list of test cases to use for the table driven tests. It is added to the current list of tests.

The test cases can be any type, as long as they are ALL the tests are of the same type, this included any tests declared

in the Cases methods to create the test object.

func (*Test) Run

func (tc *Test) Run(function TestFunc) int

Run calls the given function for each test case. (Note the function may be called again with the same testcase, if the tblTest.RunOrder option is specified.) The function must take one of four forms.

  • `func (tc $testcase)`

  • `func (tc $testcase) bool`

  • `func (idx int, tc $testcase)`

  • `func (idx int, tc $testcase) bool`

type TestCase

type TestCase interface{}

TestCase is a custom type that describes a test case.

type TestFunc

type TestFunc interface{}

TestFunc describes a function that will do the actual testing. It must take one of four forms.

  • `func (tc $testcase)`

  • `func (tc $testcase) bool`

  • `func (idx int, tc $testcase)`

  • `func (idx int, tc $testcase) bool`

Jump to

Keyboard shortcuts

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