1010import warnings
1111from test .support import captured_stderr
1212from types import ModuleType
13+ import io
14+ from contextlib import redirect_stdout
1315
1416from idlelib import pyshell as shell
1517from idlelib import run
@@ -74,18 +76,13 @@ def test_shell_show(self):
7476
7577
7678class TestDeprecatedHelp (unittest .TestCase ):
77-
78- def test_help_output (self ):
79- # Capture the help output
80- import io
81- from contextlib import redirect_stdout
82- code = r"""\
79+ CODE_SIMPLE = r"""
8380from warnings import deprecated
8481@deprecated("Test")
8582class A:
8683 pass
8784"""
88- subclass_code = r"""\
85+ CODE_SUBCLASS = r"""
8986from warnings import deprecated
9087@deprecated("Test")
9188class A:
@@ -95,17 +92,28 @@ class B(A):
9592 pass
9693b = B()
9794"""
98- module = ModuleType ("testmodule" )
99- for kode in (code ,subclass_code ):
100- exec (kode , module .__dict__ )
101- sys .modules ["testmodule" ] = module
102- f = io .StringIO ()
103- with redirect_stdout (f ):
104- help (module )
105- help_output = f .getvalue ()
106- self .assertIn ("Help on module testmodule:" , help_output )
95+ def setUp (self ):
96+ self .module = ModuleType ("testmodule" )
97+
98+ def tearDown (self ):
99+ if "testmodule" in sys .modules :
107100 del sys .modules ["testmodule" ]
108101
102+ def _get_help_output (self , code ):
103+ exec (code , self .module .__dict__ )
104+ sys .modules ["testmodule" ] = self .module
105+
106+ f = io .StringIO ()
107+ with redirect_stdout (f ):
108+ help (self .module )
109+ return f .getvalue ()
110+
111+ def test_help_output (self ):
112+ for code in (self .CODE_SIMPLE , self .CODE_SUBCLASS ):
113+ with self .subTest (code = code ):
114+ help_output = self ._get_help_output (code )
115+ self .assertIn ("Help on module testmodule:" , help_output )
116+
109117
110118if __name__ == '__main__' :
111119 unittest .main (verbosity = 2 )
0 commit comments