[TIP] Generator methods and nose: a bit lost...
Fernando Perez
fperez.net at gmail.com
Tue Apr 21 15:53:11 PDT 2009
Mmmhh, here we go again. I'm now confused by why this bit doesn't work:
"""Simple example of test generators in nose"""
import nose.tools as nt
def assert_true(*a,**kw):
return nt.assert_true(*a,**kw)
class Test1(object):
def setup(self):
self.x = 1
def test_a(self):
"t1"
nt.assert_equals(self.x, 1)
# A test generator - no docstring so nose reports arguments
def testgen1(self):
for i in range(2):
yield assert_true,True
# Another test generator, this one tries to access the instance
def testgen2(self):
yield assert_true, self.x==1
#################### EOF
When I run that simple code, the second tesgen fails:
uqbar[python]> nosetests -vv test_nosegen.py
t1 ... ok
test_nosegen.Test1.testgen1(True,) ... ok
test_nosegen.Test1.testgen1(True,) ... ok
ERROR
======================================================================
ERROR: test suite
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/fperez/usr/local/lib/python2.5/site-packages/nose-0.10.4-py2.5.egg/nose/suite.py",
line 154, in run
self.setUp()
File "/home/fperez/usr/local/lib/python2.5/site-packages/nose-0.10.4-py2.5.egg/nose/suite.py",
line 180, in setUp
if not self:
File "/home/fperez/usr/local/lib/python2.5/site-packages/nose-0.10.4-py2.5.egg/nose/suite.py",
line 65, in __nonzero__
test = self.test_generator.next()
File "/home/fperez/usr/local/lib/python2.5/site-packages/nose-0.10.4-py2.5.egg/nose/loader.py",
line 249, in generate
for test in g():
File "/home/fperez/code/python/test_nosegen.py", line 24, in testgen2
yield assert_true, self.x==1
AttributeError: 'Test1' object has no attribute 'x'
----------------------------------------------------------------------
Ran 3 tests in 0.002s
FAILED (errors=1)
uqbar[python]>
So it seems like the test generators do not honor the setup part?
The nose docs don't really say much about test objects with generators
being any different than the rest, but it seems there are some
subtleties involved... Guidance or pointers to the relevant docs would
be much appreciated.
Cheers,
f
More information about the testing-in-python
mailing list