testify icon indicating copy to clipboard operation
testify copied to clipboard

Len: Avoid printing the value of very large objects

Open Inuart opened this issue 2 years ago • 1 comments

Summary

When testing the Len of very large objects, the error message can appear very long or just not appear at all. Having a "printing length limit" helps in keeping the console clean and ensures the message should have %d item(s), but has %d appears and can be read.

Changes

A failed Len assertion will print the type of the object instead of its value if the actual length of the object is greater than 100 items. 100 is an arbitrary value and I'm open to changing it.

Motivation

In my tests some slices have thousands of items and I find myself avoiding

assert.Len(t, items, expectedItems)

in favour of

assert.True(t, len(items) == expectedItems, "expected %d items to be set", expectedItems)

just because the console gets filled with thousands of values that take all available space and are thus not useful for debugging.

In very large slices no message appears, leaving an empty error behind.

Inuart avatar Jul 20 '22 14:07 Inuart

An interesting issue I'm experiencing due to this issue is that no error is printed at all when the string representation of the slice is too long:

=== RUN   TestGetProducts
    products_test.go:48:
                Error Trace:    C:/Users/Fots/source/downloads/api/products_test.go:48
                Error:
                Test:           TestGetProducts
--- FAIL: TestGetProducts (0.01s)

In my case, the string representation had 248,649 characters.

Cheers Fotis

fgimian avatar Jan 22 '24 03:01 fgimian