Skip to content

TST: better assertion messages on test failures (GH4397) #4430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2013

Conversation

jreback
Copy link
Contributor

@jreback jreback commented Aug 1, 2013

closes #4397

a start at least

@jreback
Copy link
Contributor Author

jreback commented Aug 1, 2013

@cpcloud
cc @jtratner

good?

@cpcloud
Copy link
Member

cpcloud commented Aug 1, 2013

what about a name attr check for indices?

@cpcloud
Copy link
Member

cpcloud commented Aug 1, 2013

and maybe just pep8 this sucker while ur at it ...

@jreback
Copy link
Contributor Author

jreback commented Aug 1, 2013

I think that's handled separately (needs check_names to be true), maybe after @jtratner PR we can turn it on in full

@cpcloud
Copy link
Member

cpcloud commented Aug 1, 2013

oh ok

@cpcloud
Copy link
Member

cpcloud commented Aug 1, 2013

oh i see it's there nvm

@jreback
Copy link
Contributor Author

jreback commented Aug 1, 2013

peped

@cpcloud
Copy link
Member

cpcloud commented Aug 1, 2013

nice llooks good glad to have some more info here!

jreback added a commit that referenced this pull request Aug 1, 2013
TST: better assertion messages on test failures (GH4397)
@jreback jreback merged commit f1bac6f into pandas-dev:master Aug 1, 2013
right), "[index] left [{0}], right [{0}]".format(left, right)


def assert_attr_equal(attr, left, right):
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is as useful as what we had previously. It needs to raise when one of the components does not have an attribute (e.g., it's the wrong type altogether or something weird has happened). Further, if you do hasattr or getattr, it's not exactly equivalent to trying to get the attribute and defaulting to None otherwise. (and also I think getattr ignores any exception that occurs when trying to get it). So, I think it's better to add an assert_equal that takes an argument which explains. e.g.

assert_equal(left.column.dtype, right.column.dtype, "column dtypes did not match')

Where assert_equal is:

def assert_equal(actual, expected, msg=""):
     assert expected == actual, "%s: %r != %r" % (msg, actual, expected)

Plus, this means that the Exception that will bubbles up if something goes wrong will not be on assert_attr_equal but will instead be on the line assert_equal(left.column.dtype, right.column.dtype, msg) and you'll get the nice error message about not having an attribute and you'll see it's on the column...much easier to debug.

To illustrate the difference, check out this small example:

class SomeClass(object):
    @property
    def failing_property(self):
        return "wefawef".unreal_method(["a"])

obj = SomeClass()
print getattr(obj, "failing_property", None) # no error
print obj.failing_property # fails appropriately

And if you just change the getattr(left, attr, None) to getattr(left, attr), you still lose the nicer error output because the actual error line will read getattr(left, attr) rather than left.column.dtype

@jreback
Copy link
Contributor Author

jreback commented Aug 1, 2013

you are prob right....feel free to put this in....

my main reason for having this is the problem of

assertion failed....left.dtype == right.dtype...but have no idea what happened

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TST: use 2-form asserts in pandas.utils.testing
3 participants