Python unitest – Use variables defined in module and class level setup functions, in tests

For the string variable a, the only solution is global a. If you look at the Python 2 and Python 3 source code, setupModule() doesn’t appear to do anything magical, so all the usual namespace rules apply.

If a were a mutable variable, like a list, you could define it at global scope and then append to it within setupModule.

Variable b is easier to work with because it is defined within a class. Try this:

@classmethod
def setUpClass(cls):
    cls.b = "Setup Class variable"

def test_1(self):
    print self.b

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)