Skip to content

Commit a717c56

Browse files
bpo-33917: Fix and document idlelib/idle_test/template.py (GH-7830)
The revised file compiles, runs, and tests OK. idle_test/README.txt explains how to use it to create new IDLE test files. (cherry picked from commit 87a9273) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent b0f3526 commit a717c56

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

Lib/idlelib/idle_test/README.txt

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,27 @@ python -m idlelib.idle_test.htest
1515
1. Test Files
1616

1717
The idle directory, idlelib, has over 60 xyz.py files. The idle_test
18-
subdirectory should contain a test_xyz.py for each, where 'xyz' is
19-
lowercased even if xyz.py is not. Here is a possible template, with the
20-
blanks after '.' and 'as', and before and after '_' to be filled in.
18+
subdirectory contains test_xyz.py for each implementation file xyz.py.
19+
To add a test for abc.py, open idle_test/template.py and immediately
20+
Save As test_abc.py. Insert 'abc' on the first line, and replace
21+
'zzdummy' with 'abc.
2122

22-
import unittest
23-
from test.support import requires
24-
import idlelib. as
25-
26-
class _Test(unittest.TestCase):
23+
Remove the imports of requires and tkinter if not needed. Otherwise,
24+
add to the tkinter imports as needed.
2725

28-
def test_(self):
26+
Add a prefix to 'Test' for the initial test class. The template class
27+
contains code needed or possibly needed for gui tests. See the next
28+
section if doing gui tests. If not, and not needed for further classes,
29+
this code can be removed.
2930

30-
if __name__ == '__main__':
31-
unittest.main(verbosity=2)
32-
33-
Add the following at the end of xyy.py, with the appropriate name added
34-
after 'test_'. Some files already have something like this for htest.
35-
If so, insert the import and unittest.main lines before the htest lines.
31+
Add the following at the end of abc.py. If an htest was added first,
32+
insert the import and main lines before the htest lines.
3633

3734
if __name__ == "__main__":
38-
import unittest
39-
unittest.main('idlelib.idle_test.test_', verbosity=2, exit=False)
35+
from unittest import main
36+
main('idlelib.idle_test.test_abc', verbosity=2, exit=False)
37+
38+
The ', exit=False' is only needed if an htest follows.
4039

4140

4241

@@ -55,12 +54,14 @@ from test.support import requires
5554
requires('gui')
5655

5756
To guard a test class, put "requires('gui')" in its setUpClass function.
57+
The template.py file does this.
5858

59-
To avoid interfering with other GUI tests, all GUI objects must be destroyed and
60-
deleted by the end of the test. The Tk root created in a setUpX function should
61-
be destroyed in the corresponding tearDownX and the module or class attribute
62-
deleted. Others widgets should descend from the single root and the attributes
63-
deleted BEFORE root is destroyed. See https://bugs.python.org/issue20567.
59+
To avoid interfering with other GUI tests, all GUI objects must be
60+
destroyed and deleted by the end of the test. The Tk root created in a
61+
setUpX function should be destroyed in the corresponding tearDownX and
62+
the module or class attribute deleted. Others widgets should descend
63+
from the single root and the attributes deleted BEFORE root is
64+
destroyed. See https://bugs.python.org/issue20567.
6465

6566
@classmethod
6667
def setUpClass(cls):
@@ -75,12 +76,23 @@ deleted BEFORE root is destroyed. See https://bugs.python.org/issue20567.
7576
cls.root.destroy()
7677
del cls.root
7778

78-
The update_idletasks call is sometimes needed to prevent the following warning
79-
either when running a test alone or as part of the test suite (#27196).
79+
The update_idletasks call is sometimes needed to prevent the following
80+
warning either when running a test alone or as part of the test suite
81+
(#27196). It should not hurt if not needed.
82+
8083
can't invoke "event" command: application has been destroyed
8184
...
8285
"ttk::ThemeChanged"
8386

87+
If a test creates instance 'e' of EditorWindow, call 'e._close()' before
88+
or as the first part of teardown. The effect of omitting this depends
89+
on the later shutdown. Then enable the after_cancel loop in the
90+
template. This prevents messages like the following.
91+
92+
bgerror failed to handle background error.
93+
Original error: invalid command name "106096696timer_event"
94+
Error in bgerror: can't invoke "tk" command: application has been destroyed
95+
8496
Requires('gui') causes the test(s) it guards to be skipped if any of
8597
these conditions are met:
8698

Lib/idlelib/idle_test/template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"Test , coverage %."
22

3-
from idlelib import
3+
from idlelib import zzdummy
44
import unittest
55
from test.support import requires
66
from tkinter import Tk
@@ -23,7 +23,7 @@ def tearDownClass(cls):
2323
del cls.root
2424

2525
def test_init(self):
26-
self.assert
26+
self.assertTrue(True)
2727

2828

2929
if __name__ == '__main__':
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix and document idlelib/idle_test/template.py. The revised file compiles,
2+
runs, and tests OK. idle_test/README.txt explains how to use it to create
3+
new IDLE test files.

0 commit comments

Comments
 (0)