lightwight test environment

Build Status Coverage Status GoDoc Go Report Card Version

Should

lightweight test environment

Install

go get github.com/maprost/should

Supported methods

  • should.BeEqual(t, element, element) -> check if two elements are equal
  • should.NotBeEqual(t,element, element) -> check if two elements are not equal
  • should.BeNil(t,element) -> check if an element it nil
  • should.NotBeNilt,(element) -> check if an element is not nil
  • should.BeTrue(t,element) -> check if an element is true
  • should.BeFalse(t,element) -> check if an element is false
  • should.HaveLength(t,collection, length) -> (only for array/slice/map) check the length of the collection
  • should.Contain(t,collection, element) -> (only for array/slice/map/string) check if the collection contains the element
  • should.NotContain(t,collection, element) -> (only for array/slice/map) check if the collection contains not the element
  • should.BeSimilar(t,collection, collection) -> (only for array/slice) check if the two collections contains the same elements
  • should.NotBeSimilar(t,collection, collection) -> (only for array/slice) check if the two collections contains at least one different element
  • should.Fail -> stops the tests with the given error message

Usage

Please look into the test files to see the possibilities. For the first look here some examples:

func TestSimple(t *testing.T) {
	should.BeEqual(t, 1, 1, "should be equal")
	should.BeNil(t, nil)
	should.NotBeNil(t, 1)
	should.BeTrue(t, true)
	should.BeFalse(t, false)
	should.NotBeEqual(t, 1, 2)
	should.NotBeEqual(t, 1.0, 1)
	should.NotBeEqual(t, int64(1), int32(1))
}

func TestDataStructures(t *testing.T) {
	should.HaveLength(t, []int{1, 2, 3}, 3)
	should.HaveLength(t, [3]int{1, 2, 3}, 3)
	should.HaveLength(t, map[int]int{1: 1, 2: 3, 3: 5}, 3)
	should.BeEqual(t, []int{1, 2, 3}, []int{1, 2, 3})
	should.NotBeEqual(t, []int{1, 2, 3}, []int{3, 2, 1})
	should.BeSimilar(t, []int{1, 2, 3}, []int{3, 2, 1})
	should.BeSimilar(t, []int{1, 2, 3}, [3]int{3, 2, 1})
	should.NotBeSimilar(t, []int{1, 2, 3}, [3]int{3, 2, 4})
	should.NotBeSimilar(t, []int{1, 2, 3}, []int{2, 3, 4, 1})
	should.NotBeEqual(t, []int{1, 2, 3}, [3]int{1, 2, 3})
	should.BeEqual(t, [3]int{1, 2, 3}, [3]int{1, 2, 3})
	should.Contain(t, []int{1, 2, 3}, 2)
	should.NotContain(t, []int{1, 2, 3}, 4)
	should.Contain(t, [3]int{1, 2, 3}, 3)
	should.NotContain(t, [3]int{1, 2, 3}, 4)
	should.BeEqual(t, map[int]string{1: "1", 2: "2", 3: "3"}, map[int]string{1: "1", 2: "2", 3: "3"})
	should.NotBeEqual(t, map[int]string{1: "1", 3: "3"}, map[int]string{1: "1", 4: "4"})
	should.BeEqual(t, map[int]string{1: "1", 3: "3"}, map[int]string{3: "3", 1: "1"})
	should.Contain(t, map[int]string{1: "11", 3: "33"}, "33")
	should.NotContain(t, map[int]string{1: "11", 3: "33"}, "55")
}

func TestStructs(t *testing.T) {
	type Post struct {
		Id  int64
		Msg string
	}

	p1 := Post{Id: 12, Msg: "New"}
	p2 := Post{Id: 12, Msg: "New"}
	p3 := Post{Id: 12, Msg: "Old"}

	should.BeEqual(t, p1, p1)
	should.BeEqual(t, p1, p2)
	should.BeEqual(t, &p1, &p2)
	should.NotBeEqual(t, p1, p3)
	should.NotBeEqual(t, p1, &p1)

	should.BeEqual(t, []Post{p1}, []Post{p2})
	should.NotBeEqual(t, []Post{p1}, []Post{p3})
	should.Contain(t, []Post{p1, p2, p3}, p3)
	should.Contain(t, []*Post{&p1, &p2, &p3}, &p3)
	should.Contain(t, []*Post{&p1, &p3}, &p2)
	should.NotContain(t, []Post{p1, p2}, p3)
	should.NotContain(t, []Post{p1}, 22)
	should.NotContain(t, []Post{}, p1)
	should.NotContain(t, []*Post{&p1, &p2}, &p3)
	should.NotContain(t, []*Post{&p1, &p2}, p2)

	should.BeEqual(t, [1]Post{p1}, [1]Post{p2})
	should.NotBeEqual(t, [1]Post{p1}, [1]Post{p3})
	should.Contain(t, [2]Post{p1, p2}, p2)
	should.Contain(t, [3]*Post{&p1, &p2, &p3}, &p3)
	should.Contain(t, [2]*Post{&p1, &p3}, &p2)
	should.NotContain(t, [2]Post{p1, p2}, p3)
	should.NotContain(t, [1]Post{p1}, "blob")
	should.NotContain(t, [0]Post{}, p1)
	should.NotContain(t, [2]*Post{&p1, &p2}, &p3)
	should.NotContain(t, [2]*Post{&p1, &p2}, p1)

	should.BeEqual(t, map[int]Post{1: p1}, map[int]Post{1: p2})
	should.NotBeEqual(t, map[int]Post{1: p1}, map[int]Post{1: p3})
	should.NotBeEqual(t, map[int]Post{1: p1}, map[int]Post{2: p1})
	should.Contain(t, map[int]Post{1: p1, 2: p2}, p2)
	should.Contain(t, map[int]*Post{1: &p1, 2: &p2}, &p2)
	should.NotContain(t, map[int]Post{1: p1, 2: p2}, p3)
	should.NotContain(t, map[int]Post{1: p1}, "blob")
	should.NotContain(t, map[int]Post{}, p1)
	should.NotContain(t, map[int]*Post{1: &p1}, p1)
}

Output

The output of a failed test shows you the actual and expected value and a stacktrace.

should.go:75: Not equal:
	  actual: 1(float64)
	expected: 1(int)
	/.../src/github.com/maprost/should/failing_test.go:12 +0xd1
	/usr/local/go/src/testing/testing.go:657 +0x96
	/usr/local/go/src/testing/testing.go:697 +0x2ca
Similar Resources

A Go test assertion library for verifying that two representations of JSON are semantically equal

A Go test assertion library for verifying that two representations of JSON are semantically equal

jsonassert is a Go test assertion library for verifying that two representations of JSON are semantically equal. Usage Create a new *jsonassert.Assert

Jan 4, 2023

Ruby on Rails like test fixtures for Go. Write tests against a real database

testfixtures Warning: this package will wipe the database data before loading the fixtures! It is supposed to be used on a test database. Please, doub

Jan 8, 2023

A tool for generating self-contained, type-safe test doubles in go

counterfeiter When writing unit-tests for an object, it is often useful to have fake implementations of the object's collaborators. In go, such fake i

Jan 5, 2023

Sql mock driver for golang to test database interactions

Sql driver mock for Golang sqlmock is a mock library implementing sql/driver. Which has one and only purpose - to simulate any sql driver behavior in

Dec 31, 2022

A test-friendly replacement for golang's time package

timex timex is a test-friendly replacement for the time package. Usage Just replace your time.Now() by a timex.Now() call, etc. Mocking Use timex.Over

Dec 21, 2022

Easier way to unit test terraform

Unit testing terraform (WIP) Disclaimer Currently, the only way to compare values is using JSON query path and all types are strings. want := terraf

Aug 16, 2022

Learn Go with test-driven development

Learn Go with test-driven development

Learn Go with Tests Art by Denise Formats Gitbook EPUB or PDF Translations 中文 Português 日本語 한국어 Support me I am proud to offer this resource for free,

Jan 1, 2023

HTTP mocking to test API services for chaos scenarios

HTTP mocking to test API services for chaos scenarios

GAOS HTTP mocking to test API services for chaos scenarios Gaos, can create and provide custom mock restful services via using your fully-customizable

Nov 5, 2022

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022
Test-assignment - Test assignment with golang
Test-assignment - Test assignment with golang

test-assignment We have a two steam of data and we need to save it in the map: I

Jan 19, 2022
This repository includes consumer driven contract test for provider, unit test and counter api.

This repository includes consumer driven contract test for provider, unit test and counter api.

Feb 1, 2022
ctrsploit: A penetration toolkit for container environment

ctrsploit: A penetration toolkit for container environment

Feb 22, 2022
Test your command line interfaces on windows, linux and osx and nodes viá ssh and docker

Commander Define language independent tests for your command line scripts and programs in simple yaml files. It runs on windows, osx and linux It can

Dec 17, 2022
Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test
Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test

embedded-postgres Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test. When testing this provides

Dec 27, 2022
End to end functional test and automation framework
End to end functional test and automation framework

Declarative end to end functional testing (endly) This library is compatible with Go 1.12+ Please refer to CHANGELOG.md if you encounter breaking chan

Jan 6, 2023
Test your code without writing mocks with ephemeral Docker containers 📦 Setup popular services with just a couple lines of code ⏱️ No bash, no yaml, only code 💻

Gnomock – tests without mocks ??️ Spin up entire dependency stack ?? Setup initial dependency state – easily! ?? Test against actual, close to product

Dec 29, 2022
go-carpet - show test coverage in terminal for Go source files
go-carpet - show test coverage in terminal for Go source files

go-carpet - show test coverage for Go source files To view the test coverage in the terminal, just run go-carpet. It works outside of the GOPATH direc

Jan 8, 2023
http integration test framework

go-hit hit is an http integration test framework written in golang. It is designed to be flexible as possible, but to keep a simple to use interface f

Dec 29, 2022
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.

GoConvey is awesome Go testing Welcome to GoConvey, a yummy Go testing tool for gophers. Works with go test. Use it in the terminal or browser accordi

Dec 30, 2022