Skip to content

Show namedtuple field name in assertion error message #7527

@tekumara

Description

@tekumara
from typing import NamedTuple

class Cart(NamedTuple):
    num_items: int
    total_cost: float

def test_cart():
    assert Cart(1, 10) == Cart(1, 20)

pytest failure message:

    def test_cart():
>       assert Cart(1, 10) == Cart(1, 20)
E       assert Cart(num_item...total_cost=10) == Cart(num_item...total_cost=20)
E         At index 1 diff: 10 != 20
E         Use -v to get the full diff

It would nice if the field name was shown in the assertion failure message, rather than an index, eg:

    def test_cart():
>       assert Cart(1, 10) == Cart(1, 20)
E       assert Cart(num_item...total_cost=10) == Cart(num_item...total_cost=20)
E         At index 1 (total_cost) diff: 10 != 20
E         Use -v to get the full diff

Python 3.6.10
Pytest 5.4.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: reportingrelated to terminal output and user-facing messages and errorstype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions