Skip to content

Conversation

@Inuart
Copy link

@Inuart Inuart commented Jul 20, 2022

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.

@fgimian
Copy link

fgimian commented Jan 22, 2024

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

@dolmen dolmen changed the title Len: Avoid printing the value of very large objects assert.Len: Avoid printing the value of very large objects May 30, 2025
@dolmen dolmen added pkg-assert Change related to package testify/assert enhancement: output format Enhancement related to formatting of messages assert.Len must-rebase help wanted labels May 30, 2025
@dolmen dolmen changed the title assert.Len: Avoid printing the value of very large objects assert.Len: avoid printing the value of very large objects May 30, 2025
Copy link
Collaborator

@dolmen dolmen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need tests.

Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Len assertion should be changed to utilize assert.truncatingFormat and the size limit of truncatingFormat made useful.

This way testify has consistent behavior with long test output.

@brackendawson
Copy link
Collaborator

brackendawson commented Sep 16, 2025

#1646 is kind of related, but it addresses output so long that it isn't returned at all. It could be modified or built upon to reduce output to a sane limit rather than a possible limit.

@ccoVeille
Copy link
Collaborator

I would expect the change made here for Len to be included in #1646, and we close this old PR

@Inuart
Copy link
Author

Inuart commented Sep 17, 2025

It's been.. a while since I created this PR and it got ignored. Hopefully #1646 can solve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

assert.Len enhancement: output format Enhancement related to formatting of messages help wanted must-rebase pkg-assert Change related to package testify/assert

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants