Last active
January 18, 2018 12:33
-
-
Save sebschrader/715db1b8b6d22e43c06cc1548acc4764 to your computer and use it in GitHub Desktop.
Python unittest TestCase setUp and tearDown behavior
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
============================= test session starts ============================== | |
platform linux -- Python 3.6.3, pytest-3.3.1, py-1.5.0, pluggy-0.6.0 | |
rootdir: /tmp/tmp.qJ0GaB9IXd/build, inifile: | |
plugins: cov-2.5.1 | |
collected 5 items | |
test.py ..... [100%] | |
==================================== PASSES ==================================== | |
_________________________________ Test1.test_1 _________________________________ | |
_________________________________ Test1.test_2 _________________________________ | |
_________________________________ Test2.test_1 _________________________________ | |
_________________________________ Test2.test_2 _________________________________ | |
_____________________________ test_print_messages ______________________________ | |
----------------------------- Captured stdout call ----------------------------- | |
I'm Test1.setUpClass called with class Test1 | |
I'm Parent.setUpClass called with class Test1 | |
I'm Test1.setUp called with instance 0x0x7f3b3a3abb00 of class Test1 | |
I'm Parent.setUp called with instance 0x0x7f3b3a3abb00 of class Test1 | |
I'm Test1.test_1 called with instance 0x0x7f3b3a3abb00 of class Test1 | |
I'm Test1.tearDown called with instance 0x0x7f3b3a3abb00 of class Test1 | |
I'm Parent.tearDown called with instance 0x0x7f3b3a3abb00 of class Test1 | |
I'm Test1.setUp called with instance 0x0x7f3b3a3abcf8 of class Test1 | |
I'm Parent.setUp called with instance 0x0x7f3b3a3abcf8 of class Test1 | |
I'm Test1.test_2 called with instance 0x0x7f3b3a3abcf8 of class Test1 | |
I'm Test1.tearDown called with instance 0x0x7f3b3a3abcf8 of class Test1 | |
I'm Parent.tearDown called with instance 0x0x7f3b3a3abcf8 of class Test1 | |
I'm Test1.tearDownClass called with class Test1 | |
I'm Parent.tearDownClass called with class Test1 | |
I'm Test2.setUpClass called with class Test2 | |
I'm Parent.setUpClass called with class Test2 | |
I'm Test2.setUp called with instance 0x0x7f3b3a3abda0 of class Test2 | |
I'm Parent.setUp called with instance 0x0x7f3b3a3abda0 of class Test2 | |
I'm Test2.test_1 called with instance 0x0x7f3b3a3abda0 of class Test2 | |
I'm Test2.tearDown called with instance 0x0x7f3b3a3abda0 of class Test2 | |
I'm Parent.tearDown called with instance 0x0x7f3b3a3abda0 of class Test2 | |
I'm Test2.setUp called with instance 0x0x7f3b3a3abe48 of class Test2 | |
I'm Parent.setUp called with instance 0x0x7f3b3a3abe48 of class Test2 | |
I'm Test2.test_2 called with instance 0x0x7f3b3a3abe48 of class Test2 | |
I'm Test2.tearDown called with instance 0x0x7f3b3a3abe48 of class Test2 | |
I'm Parent.tearDown called with instance 0x0x7f3b3a3abe48 of class Test2 | |
I'm Test2.tearDownClass called with class Test2 | |
I'm Parent.tearDownClass called with class Test2 | |
=========================== 5 passed in 0.02 seconds =========================== |
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 | |
import functools | |
log = [] | |
def whoami(f): | |
@functools.wraps(f) | |
def wrapper(self): | |
if isinstance(self, type): | |
log.append(f"I'm {f.__qualname__} called with class " | |
f"{self.__qualname__}") | |
else: | |
log.append(f"I'm {f.__qualname__} called with instance " | |
f"0x{hex(id(self))} of class " | |
f"{self.__class__.__qualname__}") | |
return f(self) | |
return wrapper | |
class Parent(unittest.TestCase): | |
@classmethod | |
@whoami | |
def setUpClass(cls): | |
super().setUpClass() | |
@classmethod | |
@whoami | |
def tearDownClass(cls): | |
super().tearDownClass() | |
@whoami | |
def setUp(self): | |
super().setUp() | |
@whoami | |
def tearDown(self): | |
super().tearDown() | |
class Test1(Parent): | |
@classmethod | |
@whoami | |
def setUpClass(cls): | |
super().setUpClass() | |
@classmethod | |
@whoami | |
def tearDownClass(cls): | |
super().tearDownClass() | |
@whoami | |
def setUp(self): | |
super().setUp() | |
@whoami | |
def tearDown(self): | |
super().tearDown() | |
@whoami | |
def test_1(self): | |
pass | |
@whoami | |
def test_2(self): | |
pass | |
class Test2(Parent): | |
@classmethod | |
@whoami | |
def setUpClass(cls): | |
super().setUpClass() | |
@classmethod | |
@whoami | |
def tearDownClass(cls): | |
super().tearDownClass() | |
@whoami | |
def setUp(self): | |
super().setUp() | |
@whoami | |
def tearDown(self): | |
super().tearDown() | |
@whoami | |
def test_1(self): | |
pass | |
@whoami | |
def test_2(self): | |
pass | |
def test_print_messages(): | |
for msg in log: | |
print(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment