unittest inconsistent

Discussion in 'Python' started by Matt Haggard, Jan 6, 2010.

  1. Matt Haggard

    Matt Haggard Guest

    Can anyone tell me why this test fails?

    http://pastebin.com/f20039b17

    This is a minimal example of a much more complex thing I'm trying to
    do. I'm trying to hijack a function and inspect the args passed to it
    by another function.

    The reason the 'Tester' object has no attribute 'arg1' is because
    "self" still refers to the object made for testA.
     
    Matt Haggard, Jan 6, 2010
    #1
    1. Advertising

  2. Matt Haggard

    Phlip Guest

    On Jan 5, 4:14 pm, Matt Haggard <> wrote:
    > Can anyone tell me why this test fails?
    >
    > http://pastebin.com/f20039b17
    >
    > This is a minimal example of a much more complex thing I'm trying to
    > do.  I'm trying to hijack a function and inspect the args passed to it
    > by another function.
    >
    > The reason the 'Tester' object has no attribute 'arg1' is because
    > "self" still refers to the object made for testA.


    I hope someone else can spot the low-level reason...

    ....but why aren't you using http://pypi.python.org/pypi/mock/ ? Look
    up its patch_object facility...
     
    Phlip, Jan 6, 2010
    #2
    1. Advertising

  3. Matt Haggard

    André Guest

    On Jan 5, 8:14 pm, Matt Haggard <> wrote:
    > Can anyone tell me why this test fails?
    >
    > http://pastebin.com/f20039b17
    >
    > This is a minimal example of a much more complex thing I'm trying to
    > do.  I'm trying to hijack a function and inspect the args passed to it
    > by another function.
    >
    > The reason the 'Tester' object has no attribute 'arg1' is because
    > "self" still refers to the object made for testA.


    Quick answer: change faketest.py as follows:

    #--------------------------------------------------
    # faketest.py
    #--------------------------------------------------

    #from importme import render
    import importme

    def run(somearg):
    return importme.render(somearg)

    =========
    A long answer, with explanation, will cost you twice as much ;-)
    (but will have to wait)

    André
     
    André, Jan 6, 2010
    #3
  4. Matt Haggard

    Peter Otten Guest

    André wrote:

    > On Jan 5, 8:14 pm, Matt Haggard <> wrote:
    >> Can anyone tell me why this test fails?
    >>
    >> http://pastebin.com/f20039b17
    >>
    >> This is a minimal example of a much more complex thing I'm trying to
    >> do. I'm trying to hijack a function and inspect the args passed to it
    >> by another function.
    >>
    >> The reason the 'Tester' object has no attribute 'arg1' is because
    >> "self" still refers to the object made for testA.

    >
    > Quick answer: change faketest.py as follows:
    >
    > #--------------------------------------------------
    > # faketest.py
    > #--------------------------------------------------
    >
    > #from importme import render
    > import importme
    >
    > def run(somearg):
    > return importme.render(somearg)
    >
    > =========
    > A long answer, with explanation, will cost you twice as much ;-)
    > (but will have to wait)
    >
    > André


    Or you figure it out yourself staring at

    >>> import os
    >>> from os import rename
    >>> os.rename = 42
    >>> rename

    <built-in function rename>
    >>> os.rename

    42

    from module import name

    binds the object referred to by module.name to the name variable in the
    current module. You can think of it as a shortcut for

    import module
    name = module.name
    del module

    When you later rebind

    import module
    module.name = something_else

    the reference in the current module isn't magically updated to point to
    something_else.

    Peter
     
    Peter Otten, Jan 6, 2010
    #4
  5. Phlip wrote:
    >> The reason the 'Tester' object has no attribute 'arg1' is because
    >> "self" still refers to the object made for testA.

    >
    > I hope someone else can spot the low-level reason...
    >
    > ...but why aren't you using http://pypi.python.org/pypi/mock/ ? Look
    > up its patch_object facility...


    Indeed, I love mock, although I prefer testfixture replace decorator
    and/or context manager for installing and removing them:

    http://packages.python.org/testfixtures/mocking.html

    cheers,

    Chris

    --
    Simplistix - Content Management, Batch Processing & Python Consulting
    - http://www.simplistix.co.uk
     
    Chris Withers, Jan 12, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo Geshel
    Replies:
    2
    Views:
    427
    Neo Geshel
    Nov 3, 2003
  2. Nazir
    Replies:
    0
    Views:
    319
    Nazir
    Nov 13, 2003
  3. =?Utf-8?B?TVIuIFVOREVSSElMTA==?=

    Inconsistent look between browsers

    =?Utf-8?B?TVIuIFVOREVSSElMTA==?=, Apr 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    279
    bruce barker
    Apr 20, 2004
  4. Mike Labosh

    Inconsistent occasional error

    Mike Labosh, Jun 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    388
    Mike Labosh
    Jun 18, 2004
  5. Paul  Moore
    Replies:
    1
    Views:
    385
    Paul Moore
    Oct 14, 2008
Loading...

Share This Page