I’m going to cover nose setup and teardown fixtures at the package, module, class, method, and function level.
This isn’t about what code to put into the fixtures, just about the syntax and flow.
And a bit about naming conventions.
Although I talked about the fixtures in the nose introduction, I think this post will work better as a reference.
Method vs. function
In the discussion below, I’m distinguishing a difference between method and function.
I’m following a convention from the python documentation:
A method is a function that “belongs to” an object.
In other words, if a function is NOT in a class, it’s a function.
If it IS in a class, it’s a method.
Package level fixtures
Add ‘setup_package()’ and ‘teardown_package()’ functions to the ‘__init__.py’ of a package.
‘setup_package’ will run before any test modules within the package.
‘teardown_package’ will run after all of the modules are run (if setup succeeded).
def setup_package():
pass
def teardown_package():
pass
Module level fixtures
Module fixtures bracket the execution of everthing in the module (test classes and test functions).
These functions only run once each.
def setup_module():
pass
def teardown_module():
pass
Class level fixtures
Class fixtures bracket the execution of everthing in a class.
These functions only run once each.
class TestClass():
@classmethod
def setup_class(cls):
pass
@classmethod
def teardown_class(cls):
pass
Class method level fixtures
Class method fixtures bracket the execution of each and every test method in the class.
These functions run multiple times, once for each test method.
class TestClass():
def setup(self):
pass
def teardown(self):
pass
Function level fixtures
Function level fixtures are the oddball of the bunch.
Nose doesn’t do any naming convention detection for this.
It’s all done with the ‘@with_setup’ decorator, which has to be included from ‘nose.tools’.
from nose.tools import with_setup
def setup_function():
pass
def teardown_function():
pass
@with_setup(setup_function, teardown_function)
def test_something():
pass
This does allow you to have different fixtures for different tests.
def foo():
pass
def bar():
pass
@with_setup(foo, bar)
def test_something_else():
pass
However, please choose names that make sense.
It also allows you to forget to add the decorator, so be careful.
@with_setup(setup_function, teardown_function)
def test_a():
pass
@with_setup(setup_function, teardown_function)
def test_b():
pass
def test_c(): # Does this lack a fixture on purpose? or did someone forget?
pass
Full example
__init__.py
from __future__ import print_function
def setup_package():
print('')
print(__name__, '__init__.py : setup_package() ========================================')
def teardown_package():
print(__name__, '__init__.py : teardown_package() =====================================')
test_using_classes.py
from __future__ import print_function
def setup_module():
print(__name__, ': setup_module() ~~~~~~~~~~~~~~~~~~~~~~')
def teardown_module():
print(__name__, ': teardown_module() ~~~~~~~~~~~~~~~~~~~')
class TestClass():
@classmethod
def setup_class(cls):
print(__name__, ': TestClass.setup_class() ----------')
@classmethod
def teardown_class(cls):
print(__name__, ': TestClass.teardown_class() -------')
def setup(self):
print(__name__, ': TestClass.setup() - - - - - - - -')
def teardown(self):
print(__name__, ': TestClass.teardown() - - - - - - -')
def test_method_1(self):
print(__name__, ': TestClass.test_method_1()')
def test_method_2(self):
print(__name__, ': TestClass.test_method_2()')
test_using_functions.py
from __future__ import print_function
from nose.tools import with_setup
def setup_module():
print(__name__, ': setup_module() ~~~~~~~~~~~~~~~~~~~~~~')
def teardown_module():
print(__name__, ': teardown_module() ~~~~~~~~~~~~~~~~~~~')
def setup_function():
"attached with 'with_setup'decorator"
print(__name__, ': setup_function() - - - - - - - - -')
def teardown_function():
"attached with 'with_setup'decorator"
print(__name__, ': teardown_function() - - - - - - -')
def test_func_1():
print(__name__, ': test_func_1()')
def test_func_2():
print(__name__, ': test_func_2()')
@with_setup(setup_function, teardown_function)
def test_func_3():
print(__name__, ': test_func_3()')
Control flow
I put the files in a testNoseFixtures
directory.
Running the tests with -s
so I can see the output, produces the following.
> nosetests -s testNosePackage
.....
testNosePackage __init__.py : setup_package() ========================================
testNosePackage.test_using_classes : setup_module() ~~~~~~~~~~~~~~~~~~~~~~
testNosePackage.test_using_classes : TestClass.setup_class() ----------
testNosePackage.test_using_classes : TestClass.setup() - - - - - - - -
testNosePackage.test_using_classes : TestClass.test_method_1()
testNosePackage.test_using_classes : TestClass.teardown() - - - - - - -
testNosePackage.test_using_classes : TestClass.setup() - - - - - - - -
testNosePackage.test_using_classes : TestClass.test_method_2()
testNosePackage.test_using_classes : TestClass.teardown() - - - - - - -
testNosePackage.test_using_classes : TestClass.teardown_class() -------
testNosePackage.test_using_classes : teardown_module() ~~~~~~~~~~~~~~~~~~~
testNosePackage.test_using_functions : setup_module() ~~~~~~~~~~~~~~~~~~~~~~
testNosePackage.test_using_functions : test_func_1()
testNosePackage.test_using_functions : test_func_2()
testNosePackage.test_using_functions : setup_function() - - - - - - - - -
testNosePackage.test_using_functions : test_func_3()
testNosePackage.test_using_functions : teardown_function() - - - - - - -
testNosePackage.test_using_functions : teardown_module() ~~~~~~~~~~~~~~~~~~~
testNosePackage __init__.py : teardown_package() =====================================
----------------------------------------------------------------------
Ran 5 tests in 0.001s
OK
Alternative names
Nose is pretty forgiving about naming conventions for fixtures.
I’ll list the alternative names for the different fixtures.
However, I strongly encourage you to use the names listed above.
The names listed above are, my opinion, easiest to read.
The exception to this is setup_function/teardown_function, since those are possibly custom for every test function, use whatever you like.
- setup_package
- setup, setUp, or setUpPackage
- teardown_package
- teardown, tearDown, or tearDownPackage
- setup_module
- setup, setUp, or setUpModule
- teardown_module
- teardown, tearDown, or tearDownModule
- setup_class
- setupClass, setUpClass, setupAll, or setUpAll
- teardown_class
- teardownClass, tearDownClass, teardownAll, or tearDownAll.
- setup (class method fixtures)
- setUp
- teardown (class method fixtures)
- tearDown
- setup_function / teardown_function
- can be named anything, since it’s attached to a function with ‘@with_setup’