nose-devs/nose — Issue #406: with_setup() fails with some test generators
Author: jpellerin
Created: 2011-12-14 16:10:19+00:00
State: open

What steps will reproduce the problem?

from nose.tools import with_setup
db = []

def gen_setup():
db.append(1)

def gen_teardown():
del db[:]

@with_setup(gen_setup, gen_teardown)
def test():
first = db[0]
def check(arg):
assert arg == first
yield check, 1

What is the expected output? What do you see instead?

Initialization code inside the generator is executed before gen_setup() is called, causing an error. I expected the gen_setup() function to be called before the generator was executed.

What version of the product are you using? On what operating system?

1.0.0 on Mac OS X 10.6.6

Please provide any additional information below.

While it's easy to refactor in this very simple example to avoid the problem, there are other use cases where this type of refactoring is not practical. Consider a with_setup() type decorator that sets up a database transaction before running the tests and then rolls back after running the tests. The code inside the generator should be able to setup the database state before generating the test cases to be executed with that state, but that does not appear to be possible.

This error cause by nose.suite.ContextSuite.setUp(), which invokes LazySuite.__nonzero__(), and that generates the first test before invoking the setup routine. One potential (hackish) solution is to check an attribute on the generator to see if it needs to have its setup routine called before any test cases are generated. A better solution may be to always execute the setup routine before any tests are generated.

Google Code Info:
Issue #: 397
Author: miller...@gmail.com
Created On: 2011-02-10T15:51:21.000Z
Closed On: