Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > unittest inconsistent

Reply
Thread Tools

unittest inconsistent

 
 
Matt Haggard
Guest
Posts: n/a
 
      01-06-2010
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.
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      01-06-2010
On Jan 5, 4:14*pm, Matt Haggard <(E-Mail Removed)> 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...
 
Reply With Quote
 
 
 
 
André
Guest
Posts: n/a
 
      01-06-2010
On Jan 5, 8:14*pm, Matt Haggard <(E-Mail Removed)> 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é
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-06-2010
André wrote:

> On Jan 5, 8:14 pm, Matt Haggard <(E-Mail Removed)> 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

 
Reply With Quote
 
Chris Withers
Guest
Posts: n/a
 
      01-12-2010
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unittest - adding a doctest suite to unittest.main Paul Moore Python 1 10-14-2008 03:32 PM
Unittest and dynamically created methods JAWS Python 2 10-15-2003 08:34 AM
weird unittest result Gonçalo Rodrigues Python 3 10-01-2003 11:30 PM
How do you do unittest? Will Stuyvesant Python 5 08-02-2003 07:18 AM
unittest problems Richard Wesley Python 3 07-30-2003 10:27 PM



Advertisments