All Projects → maxatome → Go Testdeep

maxatome / Go Testdeep

Licence: bsd-2-clause
Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Go Testdeep

Kratos
A modular-designed and easy-to-use microservices framework in Go.
Stars: ✭ 15,844 (+11464.96%)
Mutual labels:  framework, toolkit
Framevuerk
Fast, Responsive, Multi Language, Both Direction Support and Configurable UI Framework based on Vue.js.
Stars: ✭ 252 (+83.94%)
Mutual labels:  framework, toolkit
Asu
facebook hacking toolkit
Stars: ✭ 197 (+43.8%)
Mutual labels:  framework, toolkit
Typescene
Strongly typed front-end framework
Stars: ✭ 197 (+43.8%)
Mutual labels:  framework, toolkit
Awesome Ui Component Library
Curated list of framework component libraries for UI styles/toolkit
Stars: ✭ 702 (+412.41%)
Mutual labels:  framework, toolkit
Testify
A toolkit with common assertions and mocks that plays nicely with the standard library
Stars: ✭ 14,996 (+10845.99%)
Mutual labels:  toolkit, assertions
Xrtk Core
The Official Mixed Reality Framework for Unity
Stars: ✭ 219 (+59.85%)
Mutual labels:  framework, toolkit
Should.js
BDD style assertions for node.js -- test framework agnostic
Stars: ✭ 1,908 (+1292.7%)
Mutual labels:  assertions, tests
Zerocode
A community-developed, free, open source, microservices API automation and load testing framework built using JUnit core runners for Http REST, SOAP, Security, Database, Kafka and much more. Zerocode Open Source enables you to create, change, orchestrate and maintain your automated test cases declaratively with absolute ease.
Stars: ✭ 482 (+251.82%)
Mutual labels:  framework, assertions
Kooper
Kooper is a simple Go library to create Kubernetes operators and controllers.
Stars: ✭ 388 (+183.21%)
Mutual labels:  framework, toolkit
Eventhus
Go - CQRS / Event Sourcing made easy - Go
Stars: ✭ 350 (+155.47%)
Mutual labels:  framework, toolkit
Libmtev
Mount Everest Application Framework
Stars: ✭ 104 (-24.09%)
Mutual labels:  framework, toolkit
Xtd forms
Modern c++17 library to create native gui for Microsoft Windows, Apple macOS and Linux.
Stars: ✭ 25 (-81.75%)
Mutual labels:  framework, toolkit
Eviltwinframework
A framework for pentesters that facilitates evil twin attacks as well as exploiting other wifi vulnerabilities
Stars: ✭ 122 (-10.95%)
Mutual labels:  framework, toolkit
Easycnn
easy convolution neural network
Stars: ✭ 136 (-0.73%)
Mutual labels:  framework
Foxify
The fast, easy to use & typescript ready web framework for Node.js
Stars: ✭ 138 (+0.73%)
Mutual labels:  framework
Http
An opinionated framework for scalable web 🌎
Stars: ✭ 136 (-0.73%)
Mutual labels:  framework
Muta
Muta is a high-performance blockchain framework.
Stars: ✭ 135 (-1.46%)
Mutual labels:  framework
Box Css Framework
Box - CSS Framework
Stars: ✭ 138 (+0.73%)
Mutual labels:  framework
Catch Exception
Stars: ✭ 137 (+0%)
Mutual labels:  assertions

go-testdeep

Build Status Coverage Status Go Report Card 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 flexible comparisons using TestDeep operators:

import (
  "testing"

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

func TestMyFunc(t *testing.T) {
  td.Cmp(t, MyFunc(), td.Struct(
    &Info{Name: "Alice"},
    td.StructFields{
      "Age": td.Between(40, 45),
    },
  ))
}

Or anchoring operators directly in literals, as in:

import (
  "testing"

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

func TestMyFunc(tt *testing.T) {
  t := td.NewT(tt)

  t.Cmp(MyFunc(), &Info{
    Name: "Alice",
    Age:  t.Anchor(td.Between(40, 45)).(int),
  })
}

To most complex one, allowing to easily test HTTP API routes, using flexible operators:

import (
  "testing"
  "time"

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

type Person struct {
  ID        uint64    `json:"id"`
  Name      string    `json:"name"`
  Age       int       `json:"age"`
  CreatedAt time.Time `json:"created_at"`
}

func TestMyApi(t *testing.T) {
  var id uint64
  var createdAt time.Time

  testAPI := tdhttp.NewTestAPI(t, myAPI) // ← ①

  testAPI.PostJSON("/person", Person{Name: "Bob", Age: 42}). // ← ②
    Name("Create a new Person").
    CmpStatus(http.StatusCreated). // ← ③
    CmpJSONBody(td.JSON(`
// Note that comments are allowed
{
  "id":         $id,             // set by the API/DB
  "name":       "Alice",
  "age":        Between(40, 45), // ← ④
  "created_at": "$createdAt",    // set by the API/DB
}`,
      td.Tag("id", td.Catch(&id, td.NotZero())),        // ← ⑤
      td.Tag("created_at", td.All(                      // ← ⑥
        td.HasSuffix("Z"),                              // ← ⑦
        td.Smuggle(func(s string) (time.Time, error) {  // ← ⑧
          return time.Parse(time.RFC3339Nano, s)
        }, td.Catch(&createdAt, td.Between(testAPI.SentAt(), time.Now()))), // ← ⑨
      )),
    ))
  if !testAPI.Failed() {
    t.Logf("The new Person ID is %d and was created at %s", id, createdAt)
  }
}
  1. the API handler ready to be tested;
  2. the POST request with automatic JSON marshalling;
  3. the expected response HTTP status should be http.StatusCreated and the line just below, the body should match the JSON operator;
  4. some operators can be embedded, like Between here;
  5. for the $id placeholder, Catch its value: put it in id variable and check it is NotZero;
  6. for the $createdAt placeholder, use the All operator. It combines several operators like a AND;
  7. check that $createdAt date ends with "Z" using HasSuffix. As we expect a RFC3339 date, we require it in UTC time zone;
  8. convert $createdAt date into a time.Time using a custom function thanks to the Smuggle operator;
  9. then Catch the resulting value: put it in createdAt variable and check it is greater or equal than testAPI.SentAt() (the time just before the request is handled) and lesser or equal than time.Now().

See tdhttp helper or the FAQ for details about HTTP API testing.

Example of produced error in case of mismatch:

error output

Description

go-testdeep is historically a go rewrite and adaptation of wonderful Test::Deep perl.

In golang, comparing data structure is usually done using reflect.DeepEqual or using a package that uses this function behind the scene.

This function works very well, but it is not flexible. Both compared structures must match exactly and when a difference is returned, it is up to the caller to display it. Not easy when comparing big data structures.

The purpose of go-testdeep, via the td package and its helpers, is to do its best to introduce this missing flexibility using "operators", when the expected value (or one of its component) cannot be matched exactly, mixed with some useful comparison functions.

See go-testdeep.zetta.rocks for details.

Installation

$ go get -u github.com/maxatome/go-testdeep

Helpers

The goal of helpers is to make use of go-testdeep even more powerful by providing common features using TestDeep operators behind the scene.

tdhttp or HTTP API testing helper

The package github.com/maxatome/go-testdeep/helpers/tdhttp provides some functions to easily test HTTP handlers.

See tdhttp documentation for details or FAQ for an example of use.

tdsuite or testing suite helper

The package github.com/maxatome/go-testdeep/helpers/tdsuite adds tests suite feature to go-testdeep in a non-intrusive way, but easily and powerfully.

A tests suite is a set of tests run sequentially that share some data.

Some hooks can be set to be automatically called before the suite is run, before, after and/or between each test, and at the end of the suite.

See tdsuite documentation for details.

tdutil aka the helper of helpers

The package github.com/maxatome/go-testdeep/helpers/tdutil allows to write unit tests for go-testdeep helpers and so provides some helpful functions.

See tdutil for details.

See also

  • testify: a toolkit with common assertions and mocks that plays nicely with the standard library
  • go-cmp: package for comparing Go values in tests

License

go-testdeep is released under the BSD-style license found in the LICENSE file in the root directory of this source tree.

Internal function deepValueEqual is based on deepValueEqual from reflect golang package licensed under the BSD-style license found in the LICENSE file in the golang repository.

Uses two files (bypass.go & bypasssafe.go) from Go-spew which is licensed under the copyfree ISC License.

Public Domain Gopher provided by Egon Elbre. The Go gopher was designed by Renee French.

FAQ

See FAQ.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].