So I have a script test.py
from nose.tools import with_setup
class Test:
@with_setup(setup_func, teardown_func)
def test(self):
print "Hello World"
Can I have setup_func() and teardown_func() defined in an init.py in the same directory as "test.py".
Basically, the objective is to have a common setup and teardown for a bunch of test cases.
There is nothing magical about
setup_funcandteardown_func. You can certainly do that by importing them from any module, if it calledinit.pyor__init__. I would suggest you put it in some other module, saytest_setup.pybecause you may not want to have those definitions at the package level of the test module, and "explicit is better than implicit" philosophy will be enforced.ADDED: However, you have a bigger problem here:
with_setupis useful only for test functions, not for test methods or inside of subclasses. As written, in your code, yoursetupfunction will not be called regardless where it is specified, see here.If you intend to make your tests as class methods, you would have to rely on
setUpandtearDownmethods of the unittest. If you wish, you can inherit your test class from a special setup class and reuse this same special class again for different tests.