0

I have a situation where for some of the tests, I require to use different setup method than I have defined for all, And for this I though to use @with_setup decorator of nose.

However this doesn't seem to be working.

code:

import unittest
from nose.tools.nontrivial import with_setup
__author__ = 'gaurang_shah1'

class Demo(unittest.TestCase):

    def setup_func(self):
        print "setup_func"

    def teardown_func(self):
        print "teardown function"

    def setUp(self):
        print "setup"

    @with_setup(setup_func, teardown_func)
    def test_setup(self):
        print "test setup"

I am expecting following output,
setup_func
test setup
teardown_func

However I am getting following output, is there anything wrong I am doing here.
setup
test setup

Gaurang
  • 171
  • 1
  • 14

1 Answers1

1

You are constructing a unittest subclass, and as such it will always use unittest.setUp and tearDown methods for the test. As described in the documentation:

Note that with_setup is useful only for test functions, not for test methods or inside of TestCase subclasses.

If you want to use @with_setup, drop the class all together:

from nose.tools.nontrivial import with_setup

def setup_func():
    print "setup_func"

def teardown_func():
    print "teardown function"

@with_setup(setup_func, teardown_func)
def test_something():
    print "test"

Or better yet, create another unittest class that does your custom setUp function.

Oleksiy
  • 6,337
  • 5
  • 41
  • 58