I can't get minimock and nosetests to play nice

M

Matthew Wilson

I'm curious if anyone has ever tried using nosetests along with
minimock.

I'm trying to get the two to play nice and not making progress. I
also wonder if I'm using minimock incorrectly.

Here's the code I want to test, saved in a file dtfun.py.

class Chicken(object):
"I am a chicken."
def x(self): return 1
def z(self): return 1

def g():

"""
Verify that we call method x on an instance of the Chicken class.

# First set up the mockery.
Now this stuff is the real test. Called Chicken()
Called instance_of_chicken.x()
"""

# This is what the function does.
c = Chicken()
c.x()

if __name__ == "__main__":

# First set up the mockery.
from minimock import Mock
Chicken = Mock('Chicken')
Chicken.mock_returns = Mock('instance_of_chicken')

# Now run the tests.
import doctest
doctest.testmod()

Here's the results when I run the code using doctest.testmod (which
passes) and nosetests --with-doctest (KABOOM):

$ python dtfun.py # Nothing is a good thing here.

$ nosetests --with-doctest dtfun.py
F
======================================================================
FAIL: Doctest: dtfun.g
----------------------------------------------------------------------
Traceback (most recent call last):
File "doctest.py", line 2112, in runTest
raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for dtfun.g
File "/home/matt/svn-checkouts/scratch/python/dtfun/dtfun.py", line 13,
in g

----------------------------------------------------------------------
File "/home/matt/svn-checkouts/scratch/python/dtfun/dtfun.py", line
22, in dtfun.g
Failed example:
g()
Expected:
Called Chicken()
Called instance_of_chicken.x()
Got nothing


----------------------------------------------------------------------
Ran 1 test in 0.015s

FAILED (failures=1)

It seems like nose isn't building the mock objects.

Any ideas?

Matt
 
J

John J. Lee

Matthew Wilson said:
I'm curious if anyone has ever tried using nosetests along with
minimock.

This has nothing to do with nose.

I'm trying to get the two to play nice and not making progress. I
also wonder if I'm using minimock incorrectly.

Here's the code I want to test, saved in a file dtfun.py.

class Chicken(object):
"I am a chicken."
def x(self): return 1
def z(self): return 1

def g():

"""
Verify that we call method x on an instance of the Chicken class.

# First set up the mockery.

This does not rebind the module global name "Chicken".

Now this stuff is the real test.
Called Chicken()
Called instance_of_chicken.x()

g() returns None, and prints nothing.

[...]
File "/home/matt/svn-checkouts/scratch/python/dtfun/dtfun.py", line
22, in dtfun.g
Failed example:
g()
Expected:
Called Chicken()
Called instance_of_chicken.x()
Got nothing

.... because g() returns None, and prints nothing.

[...]
Any ideas?

Read the Python tutorial, write some Python code and some doctests
without using minimock. Google about Python scoping rules. Read the
doctest docs about the global namespace seen by doctest-ed code:

http://docs.python.org/lib/doctest-execution-context.html


John
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top