Skip to content

Documentation: misleading deep inspection #411

Closed
@pytestbot

Description

@pytestbot

Originally reported by: BitBucket: PeterSlickers, GitHub: PeterSlickers


I recently started using pytest und thus read some the documentation.

On page "http://pytest.org/latest/assert.html" in section "Defining your own assertion comparison" I found an example 'test_foocompare.py'.

class Foo:
    def __init__(self, val):
         self.val = val

 f1 = Foo(1)
 f2 = Foo(2)
 assert f1 == f2

Class 'foo' is very simple and it does not define any of Pythons magic methods for comparison like ne, cmp etc. Thus, a comparison of two instances of Foo will be accomplished by comparing the unique ID of the two objects:

id(f1) == id(f2)

Even if both instances are initiated with the same value, the comparison for equality will fail:

id(Foo(1)) == id(Foo(1))  --> False

Applying the deep inspection produces the following misleading result:

'=================================================== FAILURES ===================================================
_________________________________________________ test_compare _________________________________________________

    def test_compare():
        f1 = Foo(1)
        f2 = Foo(1)
>       assert f1 == f2
E    assert Comparing Foo instances:
E         vals: 1 != 1

test_foocompare.py:9: AssertionError
1 failed in 0.01 seconds

With best regards

Peter


Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributortype: docsdocumentation improvement, missing or needing clarificationtype: 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