Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosDomingues/b002d9af55ff86345fdabe201362fe33 to your computer and use it in GitHub Desktop.
Save CarlosDomingues/b002d9af55ff86345fdabe201362fe33 to your computer and use it in GitHub Desktop.
Running SetUp a single time on Python unittests
import unittest
from my_program import MyClass
class MyClassTest(unittest.TestCase):
# First define a class variable that determines
# if setUp was ever run
ClassIsSetup = False
def setUp(self):
# If it was not setup yet, do it
if not self.ClassIsSetup:
print "Initializing testing environment"
# run the real setup
self.setupClass()
# remember that it was setup already
self.__class__.ClassIsSetup = True
def setupClass(self):
# Do the real setup
unittest.TestCase.setUp(self)
# you want to have persistent things to test
self.__class__.myclass = MyClass()
# (you can call this later with self.myclass)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment