bobber6467 / python-nose

Automatically exported from code.google.com/p/python-nose
0 stars 0 forks source link

with_setup() fails with some test generators #397

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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.

Original issue reported on code.google.com by miller...@gmail.com on 10 Feb 2011 at 3:51