Skip to content

Global variables are not explained on failure #171

Description

@pytestbot

Originally reported by: Floris Bruynooghe (BitBucket: flub, GitHub: flub)


A global variable is not expanded in the explanation on failure:

var = 7

def test_global():
    val = 42
    assert var == val

Results in:

$ py.test test_global.py 
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.2.1
collected 1 items 

test_global.py F

=================================== FAILURES ===================================
_________________________________ test_global __________________________________

    def test_global():
        val = 42
>       assert var == val
E       assert var == 42

test_global.py:6: AssertionError
=========================== 1 failed in 0.01 seconds ===========================

I would expect the "E" line to say "assert 7 == 42".


Activity

  1. pytestbot commented on Nov 7, 2012

    @pytestbot
    ContributorAuthor

    Original comment by Benjamin Peterson (BitBucket: gutworth, GitHub: gutworth):


    Assertion rewriting has a heuristic inherited from the original expression evaluater that global names reprs are not given. I don't know what the purpose of this was. Someone should ask Armin perhaps.

  2. pytestbot commented on Nov 8, 2012

    @pytestbot
    ContributorAuthor

    Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


    I think in some cases it can result in too verbose output, especially when using builtins where you don't really want to have extra information on "len", "callable" etc. But even if this cannot be cut out, it's still useful to present globals. Not a big code change, is it?

  3. pytestbot commented on Nov 11, 2012

    @pytestbot
    ContributorAuthor

    Original comment by Benjamin Peterson (BitBucket: gutworth, GitHub: gutworth):


    No, it's not difficult to change. Shall I just enable reprs of global variables?

  4. pytestbot commented on Nov 12, 2012

    @pytestbot
    ContributorAuthor

    Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


    yes let's just enable it.

  5. pytestbot commented on Nov 12, 2012

    @pytestbot
    ContributorAuthor

    Original comment by Floris Bruynooghe (BitBucket: flub, GitHub: flub):


    I started doing this this weekend but just made more tests fail, I now realise I was making it more complicated then needed (I think). In visit_Name I was trying to create a new test which checked the name was not in the builtin names (and failed). However it's probably fine and easier to just remove the test for globals/locals.

    Anyway, I don't mind if you make this change. My knowledge of how to build code using AST is rather limited...

  6. pytestbot commented on Nov 12, 2012

    @pytestbot
    ContributorAuthor

    Original comment by Benjamin Peterson (BitBucket: gutworth, GitHub: gutworth):


    Another reason for this heuristic occurred to me: Many commonly used global names like classes and modules are more nicely displayed by their name rather than their repr.

  7. pytestbot commented on Jan 10, 2013

    @pytestbot
    ContributorAuthor

    Original comment by Floris Bruynooghe (BitBucket: flub, GitHub: flub):


    The attached patch has one test failure:

    __________________________________ TestAssertionRewrite.test_attribute ___________________________________
    
    self = <test_assertrewrite.TestAssertionRewrite instance at 0x1210440>
    
        def test_attribute(self):
            class X(object):
                g = 3
            ns = {"X" : X, "x" : X()}
            def f():
                assert not x.g
    >       assert getmsg(f, ns) == """assert not 3
         +  where 3 = x.g"""
    E       assert 'assert not 3... 0x12d4d90>.g' == 'assert not 3\...where 3 = x.g'
    E           assert not 3
    E         -  +  where 3 = <test_assertrewrite.X object at 0x12d4d90>.g
    E         +  +  where 3 = x.g
    
    testing/test_assertrewrite.py:245: AssertionError
    

    Which is exactly the last issue you mention, I think it might be possible to add another test to the display logic which checks if the name is a class and is a global. Do you reckon this is the right approach to keep persuing? It'll probably need a special case for a module as well.

  8. pytestbot commented on Jan 10, 2013

    @pytestbot
    ContributorAuthor

    Original comment by Benjamin Peterson (BitBucket: gutworth, GitHub: gutworth):


    display the repr of some global names (fixes #171)

  9. pytestbot commented on Jan 10, 2013

    @pytestbot
    ContributorAuthor

    Original comment by Benjamin Peterson (BitBucket: gutworth, GitHub: gutworth):


    Sorry, Floris, I didn't see your patch until now. I implemented a slightly different scheme, where we show the repr if the object doesn't have a name and isn't callable. Tell me if my heuristic works well.

  10. added a commit that references this issue on May 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions