Test Fixtures
Go to file
2024-05-09 09:39:50 -07:00
fixture fix module/package paths 2024-05-08 21:32:31 +00:00
fixtures remove junk 2024-05-09 09:39:50 -07:00
md fix module/package paths 2024-05-08 21:32:31 +00:00
td fix module/package paths 2024-05-08 21:32:31 +00:00
.gitignore Initial commit 2024-05-08 20:15:33 +00:00
go.mod update module path 2024-05-08 20:59:13 +00:00
go.sum import testing package 2024-05-08 13:28:36 -07:00
LICENSE import testing package 2024-05-08 13:28:36 -07:00
Makefile fix repo path 2024-05-08 21:08:10 +00:00
README.md import testing package 2024-05-08 13:28:36 -07:00

Test Fixtures in Go

The purpose of this package is to provide a simple tool for creating test fixtures for go tests.

What is a fixture?

A test fixture is generally used as a mechanism to provide consistent, repeatable state when testing software.

Adding Fixtures

A fixture can be as simple as a function that returns a static value or it can leverage parameters to run the same test with different values. You can also provide a matching result set to make it easier to verify the expected results.

// A fixture returns some data, often based on some input parameter
func FixtureExample(p fixture.Param) interface{} {
  return fmt.Sprintf("data %s", p)
}

func TestExample(t *testing.T) {
  // you may define a result set for verification
  r := fixture.R([]fixture.Result{"I do not like your data here","I do not like your data there","I do not like your data anywhere"})

  // creating a parameterized fixture will cause the test to be executed for parameter
  p := fixture.P([]fixture.Param{"here","there","anywhere"})

  f := fixture.New(t, FixtureExample, p, r)

  // The test body will be run  3 times (once for each parameter)
  f.RunWith(
    func (t *testing.T) {
      fv := f.Fixture()
      test_result := fmt.Sprintf("I do not like your %s", fv)
      // Verify the test a result that matches the result set
      f.AssertStrEq(test_result)
  })

}