stretchr/testify

Add NotEqualValues assertion and FailNowDiff

Open

#640 创建于 2018年7月23日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)Go (1,704 fork)batch import
help wanted

仓库指标

Star
 (25,958 star)
PR 合并指标
 (平均合并 6天 15小时) (30 天内合并 2 个 PR)

描述

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...)
}

贡献者指南