I am running a couple of nosetests
with test cases in different modules (files) each containing different tests.
I want to define a function/method that is only called once during the execution with nosetest
.
I looked at the documentation (and e.g. here) and see there are methods like setup_module
etc. - but where and how to use them? Put them into my __init__.py
? Something else?
I tried to use the following:
class TestSuite(basicsuite.BasicSuite):
def setup_module(self):
print("MODULE")
...
but this printout is never done when I run the test with nosetest
. I also do not derive from unittest.TestCase
(which will result in errors).
When looking at a package level, you can define a function named setup
in the __init__.py
of that package. Calling the tests in this package, the setup
function in the __init__.py
is called once.
Example setup
- package
- __init__.py
- test1.py
- test2.py
See documentation section 'Test packages'.
Try this one
from nose import with_setup
def my_setup_function():
print ("my_setup_function")
def my_teardown_function():
print ("my_teardown_function")
@with_setup(my_setup_function, my_teardown_function)
def test_my_cool_test():
assert my_function() == 'expected_result'
Holp it helps ^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With