Created
June 8, 2018 16:27
-
-
Save CarlosDomingues/b002d9af55ff86345fdabe201362fe33 to your computer and use it in GitHub Desktop.
Running SetUp a single time on Python unittests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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