testify icon indicating copy to clipboard operation
testify copied to clipboard

Inconsistent Formatting between Equal/NotEqual and Less/Greater/LessOrEqual/GreaterOrEqual

Open cheinani2x opened this issue 4 years ago • 1 comments

Edit: It looks like there are two PRs already open to fix this issue.

For the sake of brevity, I will reduce this example to Equal and Less, but Equal and NotEqual behave the same and Greater, Less, GreaterOrEqual and LessOrEqual behave the same; however, I wrote a full example on the Go Playground. I see the same behavior in my code and in the Go playground.

I am reporting against version 1.6.1 running Go 1.14.

The issues are

  1. Equal and Less display errors differently.
  2. There seems to be no difference between the XXX and XXXf functions and the docs are unclear about any differences.

Code:

package main

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestLess(t *testing.T) {
	assert.Less(t, 3, 2, "Message %s-%d", "42", 42)
	assert.Lessf(t, 3, 2, "Message %s-%d", "42", 42)
}

func TestEqual(t *testing.T) {
	assert.Equal(t, 1, 2, "Message %s-%d", "42", 42)
	assert.Equalf(t, 1, 2, "Message %s-%d", "42", 42)
}
Actual Output:
=== RUN   TestLess
    assertion_compare.go:230: 
        	Error Trace:	prog.go:9
        	Error:      	"3" is not less than "2"
        	Test:       	TestLess
        	Messages:   	[Message %s-%d 42 42]
    assertion_compare.go:230: 
        	Error Trace:	prog.go:10
        	Error:      	"3" is not less than "2"
        	Test:       	TestLess
        	Messages:   	[Message %s-%d 42 42]
--- FAIL: TestLess (0.00s)
=== RUN   TestEqual
    prog.go:14: 
        	Error Trace:	prog.go:14
        	Error:      	Not equal: 
        	            	expected: 1
        	            	actual  : 2
        	Test:       	TestEqual
        	Messages:   	Message 42-42
    prog.go:15: 
        	Error Trace:	prog.go:15
        	Error:      	Not equal: 
        	            	expected: 1
        	            	actual  : 2
        	Test:       	TestEqual
        	Messages:   	Message 42-42
--- FAIL: TestEqual (0.00s)
FAIL

2 tests failed.

Expected Output:

  • TestLess should not output a trace from the Testify Library, i.e. assertion_compare.go:230
  • TestLess should format the output correctly, at least in the assert.Lessf case.
  • The docs should explain what the non-formatted function additional arguments do.

I tried the documentation example as well and it failed the same way.

func TestDocExamples(t *testing.T) {
	assert.Lessf(t, 3, 2, "error message %s", "formatted")
	assert.Lessf(t, float64(3), float64(2), "error message %s", "formatted")
	assert.Lessf(t, "c", "b", "error message %s", "formatted")
}

cheinani2x avatar Dec 24 '20 10:12 cheinani2x

I believe this issue is now fixed (by https://github.com/stretchr/testify/pull/1026 and https://github.com/stretchr/testify/pull/1150), so can be closed.

lambdanis avatar Apr 21 '22 21:04 lambdanis