Simplify individual tests by defining setUp and tearDown methods.
This commit is contained in:
parent
60b15d20f2
commit
0d8372bdb9
|
@ -5,86 +5,52 @@ import atexit
|
||||||
from test import test_support
|
from test import test_support
|
||||||
|
|
||||||
class TestCase(unittest.TestCase):
|
class TestCase(unittest.TestCase):
|
||||||
def test_args(self):
|
def setUp(self):
|
||||||
# be sure args are handled properly
|
|
||||||
s = StringIO.StringIO()
|
s = StringIO.StringIO()
|
||||||
sys.stdout = sys.stderr = s
|
sys.stdout = sys.stderr = self.subst_io = s
|
||||||
save_handlers = atexit._exithandlers
|
self.save_handlers = atexit._exithandlers
|
||||||
atexit._exithandlers = []
|
atexit._exithandlers = []
|
||||||
try:
|
|
||||||
atexit.register(self.h1)
|
def tearDown(self):
|
||||||
atexit.register(self.h4)
|
sys.stdout = sys.__stdout__
|
||||||
atexit.register(self.h4, 4, kw="abc")
|
sys.stderr = sys.__stderr__
|
||||||
atexit._run_exitfuncs()
|
atexit._exithandlers = self.save_handlers
|
||||||
finally:
|
|
||||||
sys.stdout = sys.__stdout__
|
def test_args(self):
|
||||||
sys.stderr = sys.__stderr__
|
atexit.register(self.h1)
|
||||||
atexit._exithandlers = save_handlers
|
atexit.register(self.h4)
|
||||||
self.assertEqual(s.getvalue(), "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n")
|
atexit.register(self.h4, 4, kw="abc")
|
||||||
|
atexit._run_exitfuncs()
|
||||||
|
self.assertEqual(self.subst_io.getvalue(),
|
||||||
|
"h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n")
|
||||||
|
|
||||||
def test_badargs(self):
|
def test_badargs(self):
|
||||||
s = StringIO.StringIO()
|
atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
|
||||||
sys.stdout = sys.stderr = s
|
self.assertRaises(TypeError, atexit._run_exitfuncs)
|
||||||
save_handlers = atexit._exithandlers
|
|
||||||
atexit._exithandlers = []
|
|
||||||
try:
|
|
||||||
atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
|
|
||||||
self.assertRaises(TypeError, atexit._run_exitfuncs)
|
|
||||||
finally:
|
|
||||||
sys.stdout = sys.__stdout__
|
|
||||||
sys.stderr = sys.__stderr__
|
|
||||||
atexit._exithandlers = save_handlers
|
|
||||||
|
|
||||||
def test_order(self):
|
def test_order(self):
|
||||||
# be sure handlers are executed in reverse order
|
atexit.register(self.h1)
|
||||||
s = StringIO.StringIO()
|
atexit.register(self.h2)
|
||||||
sys.stdout = sys.stderr = s
|
atexit.register(self.h3)
|
||||||
save_handlers = atexit._exithandlers
|
atexit._run_exitfuncs()
|
||||||
atexit._exithandlers = []
|
self.assertEqual(self.subst_io.getvalue(), "h3\nh2\nh1\n")
|
||||||
try:
|
|
||||||
atexit.register(self.h1)
|
|
||||||
atexit.register(self.h2)
|
|
||||||
atexit.register(self.h3)
|
|
||||||
atexit._run_exitfuncs()
|
|
||||||
finally:
|
|
||||||
sys.stdout = sys.__stdout__
|
|
||||||
sys.stderr = sys.__stderr__
|
|
||||||
atexit._exithandlers = save_handlers
|
|
||||||
self.assertEqual(s.getvalue(), "h3\nh2\nh1\n")
|
|
||||||
|
|
||||||
def test_sys_override(self):
|
def test_sys_override(self):
|
||||||
# be sure a preset sys.exitfunc is handled properly
|
# be sure a preset sys.exitfunc is handled properly
|
||||||
save_handlers = atexit._exithandlers
|
|
||||||
atexit._exithandlers = []
|
|
||||||
exfunc = sys.exitfunc
|
exfunc = sys.exitfunc
|
||||||
sys.exitfunc = self.h1
|
sys.exitfunc = self.h1
|
||||||
reload(atexit)
|
reload(atexit)
|
||||||
s = StringIO.StringIO()
|
|
||||||
sys.stdout = sys.stderr = s
|
|
||||||
try:
|
try:
|
||||||
atexit.register(self.h2)
|
atexit.register(self.h2)
|
||||||
atexit._run_exitfuncs()
|
atexit._run_exitfuncs()
|
||||||
finally:
|
finally:
|
||||||
sys.stdout = sys.__stdout__
|
|
||||||
sys.stderr = sys.__stderr__
|
|
||||||
atexit._exithandlers = save_handlers
|
|
||||||
sys.exitfunc = exfunc
|
sys.exitfunc = exfunc
|
||||||
self.assertEqual(s.getvalue(), "h2\nh1\n")
|
self.assertEqual(self.subst_io.getvalue(), "h2\nh1\n")
|
||||||
|
|
||||||
def test_raise(self):
|
def test_raise(self):
|
||||||
# be sure raises are handled properly
|
atexit.register(self.raise1)
|
||||||
s = StringIO.StringIO()
|
atexit.register(self.raise2)
|
||||||
sys.stdout = sys.stderr = s
|
self.assertRaises(TypeError, atexit._run_exitfuncs)
|
||||||
save_handlers = atexit._exithandlers
|
|
||||||
atexit._exithandlers = []
|
|
||||||
try:
|
|
||||||
atexit.register(self.raise1)
|
|
||||||
atexit.register(self.raise2)
|
|
||||||
self.assertRaises(TypeError, atexit._run_exitfuncs)
|
|
||||||
finally:
|
|
||||||
sys.stdout = sys.__stdout__
|
|
||||||
sys.stderr = sys.__stderr__
|
|
||||||
atexit._exithandlers = save_handlers
|
|
||||||
|
|
||||||
### helpers
|
### helpers
|
||||||
def h1(self):
|
def h1(self):
|
||||||
|
|
Loading…
Reference in New Issue