Skip to content

assert.Len: avoid printing the value of very large objectsΒ #1801

@brackendawson

Description

@brackendawson

From #1235 by @Inuart

Description

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.

Proposed solution

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.

Use case

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions