Vincent Catalano Vincent Catalano - 3 months ago 9
Python Question

Nose test single setup function called once

How do I create a single setup function all my nose test cases that is only called once during initialization? I have a global configuration that only needs to be set once and I feel that adding the following to each module (even calling a setup function for each module) is a bit superfluous:

def setUp(self):
Configuration.configure('some configuration settings')

Answer

I figured it out! Nose provides package-level setup and teardown as documented here. All I have to do is define the setup method in the package's __init__.py file.

Here, you can see an example of how to use the setup function. To make things simple:

lines = []
def setup():
    global lines
    lines.append('test') # here, we can trigger a build
                         # and read in a file, for example

def test_this():
    assert lines[0] == 'test'