8000 unittest: Add setUpClass and tearDownClass handling. · micropython/micropython-lib@2d61dbd · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d61dbd

Browse files
Steve Lipi-anl
Steve Li
authored andcommitted
unittest: Add setUpClass and tearDownClass handling.
Supports setUp and tearDown functionality at Class level.
1 parent ddeb9a7 commit 2d61dbd

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

python-stdlib/unittest/test_unittest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,24 @@ def test_subtest_even(self):
157157
self.assertEqual(i % 2, 0)
158158

159159

160+
class TestUnittestSetup(unittest.TestCase):
161+
class_setup_var = 0
162+
163+
def setUpClass(self):
164+
TestUnittestSetup.class_setup_var += 1
165+
166+
def tearDownClass(self):
167+
# Not sure how to actually test this, but we can check (in the test case below)
168+
# that it hasn't been run already at least.
169+
TestUnittestSetup.class_setup_var = -1
170+
171+
def testSetUpTearDownClass_1(self):
172+
assert TestUnittestSetup.class_setup_var == 1, TestUnittestSetup.class_setup_var
173+
174+
def testSetUpTearDownClass_2(self):
175+
# Test this twice, as if setUpClass() gets run like setUp() it would be run twice
176+
assert TestUnittestSetup.class_setup_var == 1, TestUnittestSetup.class_setup_var
177+
178+
160179
if __name__ == "__main__":
161180
unittest.main()

python-stdlib/unittest/unittest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ def run_suite(c, test_result, suite_name=""):
366366
o = c()
367367
else:
368368
o = c
369+
set_up_class = getattr(o, "setUpClass", lambda: None)
370+
tear_down_class = getattr(o, "tearDownClass", lambda: None)
369371
set_up = getattr(o, "setUp", lambda: None)
370372
tear_down = getattr(o, "tearDown", lambda: None)
371373
exceptions = []
@@ -410,6 +412,8 @@ def run_one(test_function):
410412
except AttributeError:
411413
pass
412414

415+
set_up_class()
416+
413417
if hasattr(o, "runTest"):
414418
name = str(o)
415419
run_one(o.runTest)
@@ -426,6 +430,8 @@ def run_one(test_function):
426430
name = o.__name__
427431
run_one(o)
428432

433+
tear_down_class()
434+
429435
return exceptions
430436

431437

0 commit comments

Comments
 (0)
0