[py-dev] generative tests : setup/teardown?

holger krekel holger at merlinux.de
Tue Jul 3 13:09:36 CEST 2007


Hi Anurag! 

On Tue, Jul 03, 2007 at 03:53 -0700, anurag uniyal wrote:
> Hi,
> 
> I have just changed our testing framework from PyUnit to py.tests and so I am very new to it.
> 
> I was trying to write some generative tests so that my mutiple tests can be easily generated as they differ only by few parameters.
> It works fine except that it seem generative tests are not treated equal to normal tests (test_*)
> because setup_method is not called every time for each generated tests and that makes it impossible for me to use generative tests in my case.
> 
> Is it expected behaviour? what is the alternative?

it's known and expected behaviour.  The setup/teardown mechanics apply 
to the generator which yields/generates the actual test functions/params. 

However, you might call setup/teardown methods directly from
the generated tests.  Find below a code example which you can
run with "py.test test_gensetup.py" if you write it to a file. 

hope it helps. 

best, 

holger

---- test_gensetup.py ---- 


def mysetup(para):
    print "mysetup gentest", para

def myteardown(para):
    print "myteardown gentest", para

def gentest(para):
    assert 4 == para

def gentestwrapper(para):
    mysetup(para)  # could also call existing setup_function()/setup_method()
    try:
        gentest(para)
    finally:
        myteardown(para)

def test_generated():
    for x in 1,2,3:
        yield gentestwrapper, x




More information about the Pytest-dev mailing list