DEV Community

Maxime Soulé
Maxime Soulé

Posted on

go-testdeep v1.4.0 released

As a powerful and two-year-old testing framework, go-testdeep allows you to deeply test any golang data structure. It is fully integrated with testing standard package.

import (
  "testing"
  "github.com/maxatome/go-testdeep/td"
)

func TestLoadPerson(t *testing.T) {
  // LoadPerson() (Person, error)
  person, err := LoadPerson()
  if td.CmpNoError(t, err) {
    // person has to match exactly
    td.Cmp(t, person, Person{Name: "Bob", Age: 24})
  }
}

But many testing frameworks allow you to do this kind of comparison, no need for a new one!

As go-testdeep uses its own function to deeply compare structures, it allows you to use 60 operators for more flexibility.

Take the Bag operator as an example:

import (
  "testing"
  "github.com/maxatome/go-testdeep/td"
)

func TestLoadPersons(t *testing.T) {
  // LoadPersons(string) ([]Person, error)
  persons, err := LoadPersons("B*")
  if td.CmpNoError(t, err) {
    // persons slice has to contains these 4 persons
    // in whatever order
    td.Cmp(t, persons,
      td.Bag(
        Person{Name: "Bob", Age: 24},
        Person{Name: "Britt", Age: 29},
        Person{Name: "Barbara", Age: 35},
        Person{Name: "Brian", Age: 21},
      ))
  }
}

It is just an example, as the tests can be much more complex. See:

  • the synopsis;
  • an example from classic approach to the go-testdeep ones;
  • the FAQ, it contains some examples too.

The tdhttp helper package allows you to easily test your HTTP API using these operators.
See the example of a simple JSON/XML HTTP API tested using go-testdeep and its tdhttp package under the Go Play Space: https://goplay.space/#xrGKu2cNQFr

It reports colored error messages when something goes wrong, here used with another operator, Between:

Example of error

All the functions and methods are fully documented, each operator comes with its examples, and last but not least the test code coverage is close to 100%.

Do not hesitate to comment, file an issue or a PR. I would be happy to help you or simply discuss.

Enjoy!

homegodoc

GitHub logo maxatome / go-testdeep

Extremely flexible golang deep comparison, extends the go testing package and tests HTTP APIs

go-testdeep

Build Status Coverage Status Go Report Card GolangCI GoDoc Version Mentioned in Awesome Go

go-testdeep

Extremely flexible golang deep comparison, extends the go testing package.

Latest news

Synopsis

Make golang tests easy, from simplest usage:

import (
  "testing"

  "github.com/maxatome/go-testdeep/td"
)

func TestMyFunc(t *testing.T) {
  td.Cmp(t, MyFunc(), &Info{Name: "Alice", Age: 42})
}

To a bit more complex one, allowing…

Top comments (0)