Skip to content

Latest commit

 

History

History
170 lines (116 loc) · 4.33 KB

README.md

File metadata and controls

170 lines (116 loc) · 4.33 KB

Actually

actually report card Go Reference

A testing library focused on turning failure into success, actually.

  • Builder interface to make test code obvious
  • Consistent method name to reduce things you have to remember
  • Specific fail report to save your time
  • There are helpers to see details of a failure

Usage

package main

import (
	"testing"

	a "github.com/bayashi/actually"
	pb "github.com/bayashi/actually/testpb"
)

func TestObject(t *testing.T) {
	love, err := getLove()

	a.Got(err).NoError(t)
	a.Got(love).True(t)
}

func getLove() (bool, error) {
	return true, nil
}

func TestObjects(t *testing.T) {
	x := map[string]int{
		"foo": 123,
	}
	y := map[string]int{
		"foo": 123,
	}

	// `Same` method verifies that two objects are same in value and type.
	// Function type value is not acceptable. And not verify pointer address.
	// It will be fail, int(1) and uint(1), because of type.
	a.Got(x).Expect(y).Same(t)

	// Cmp method gets the differences between two objects by go-cmp.Diff.
	a.Got(x).Expect(y).Cmp(t)
}

func TestProtoMessages(t *testing.T) {
	x := &pb.Foo{Id: 123}
	y := &pb.Foo{Id: 123}

	// CmpProto method gets the differences between two Protobuf messages
	// by go-cmp.Diff with protocmp.Transform option.
	a.Got(x).Expect(y).CmpProto(t)

	a.Got(x).Expect(y).SamePointer(t) // This test will be failed
}

Assertions

  • True, False, Nil, NotNil, NoError
  • Same, SamePointer, SameType, SameConvertibleNumber
  • NotSame, NotSamePointer, NotSameType, NotSameConvertibleNumber
  • Cmp, CmpProto, CmpAllowUnexported, CmpIgnoreUnexported, (CmpOpt)
  • Panic, NoPanic, PanicMessage
  • Match, NotMatch
  • Len

Here is a Wiki of full API documentation.


Fail reports

actually will help you with evident fail report:

package foo

import (
	"testing"

	a "github.com/bayashi/actually"
)

func Test(t *testing.T) {
	x := "foo\nbar\nbaz"
	y := "foo\nbar\nbug"

	a.Got(x).Expect(y).Same(t)
}

Above code will put fail report like below:

=== RUN   Test
    foo_test.go:10:
                Test name:      Test
                Trace:          /path/to/foo_test.go:10
                Fail reason:    Not same value
                Type:           Expect:string, Got:string
                Expected:       "foo\nbar\nbug"
                Actually got:   "foo\nbar\nbaz"
                Diff details:   --- Expected
                                +++ Actually got
                                @@ -2,2 +2,2 @@
                                 bar
                                -bug
                                +baz

--- FAIL: Test (0.00s)

There are some helpful methods.

actually has the Debug("label", any_variable) method to show additional data only in fail report.

Like below, src variable will be dumped nicely only on fail.

res := someFunc(src)
actually.Got(res).Debug("src", src).True(t)

See more helper functions.


Installation

go get github.com/bayashi/actually

License

MIT License

Author

Dai Okabayashi: https://github.com/bayashi

Special Thanks

Inspired by: