stretchr/testify

Add NotEqualValues assertion and FailNowDiff

Open

#640 建立於 2018年7月23日

在 GitHub 查看
 (1 留言) (0 反應) (0 負責人)Go (25,958 star) (1,704 fork)batch import
help wanted

描述

Currently there is an EqualValues assertion, which seems good for checking structs are the same. But if I want to test the negative case (NotEqualValues), there doesn't seem to be a good method. It would be basically the same as the EqualValues, though just changing the inner check:

From:

func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
..snip..
if !ObjectsAreEqualValues(expected, actual) {
..snip..

To:

func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
..snip..
if ObjectsAreEqualValues(expected, actual) {
..snip..

Perhaps a refactor to share the common code in an unexported method with a flag on the test case would be better too.

I can make my own using ObjectsAreEqual and Condition, but then I don't get the niceness of the diff format in the output, which I can't access to make my own similar (without copying code) as the diff functions aren't exported.

It would also be nice if there was a wrapper function to access the diff features, perhaps something like (based on the error diff code in EqualValues:

func FailNowDiff(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
	diff := diff(expected, actual)
	expected, actual = formatUnequalValues(expected, actual)
	return Fail(t, fmt.Sprintf("Not equal: \n"+
		"expected: %s\n"+
		"actual  : %s%s", expected, actual, diff), msgAndArgs...)
}

貢獻者指南