Search code examples
pythonnose

How to define a setup method only called once during testing with nosetest?


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).


Solution

  • 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'.