bpo-33917: Fix and document idlelib/idle_test/template.py (GH-7830) · python/cpython@7c853d0 (original) (raw)

`@@ -15,28 +15,27 @@ python -m idlelib.idle_test.htest

`

15

15

`1. Test Files

`

16

16

``

17

17

`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.

`

21

22

``

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.

`

27

25

``

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.

`

29

30

``

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.

`

36

33

``

37

34

`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.

`

40

39

``

41

40

``

42

41

``

`@@ -55,12 +54,14 @@ from test.support import requires

`

55

54

`requires('gui')

`

56

55

``

57

56

`To guard a test class, put "requires('gui')" in its setUpClass function.

`

``

57

`+

The template.py file does this.

`

58

58

``

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.

`

64

65

``

65

66

` @classmethod

`

66

67

` def setUpClass(cls):

`

`@@ -75,12 +76,23 @@ deleted BEFORE root is destroyed. See https://bugs.python.org/issue20567.

`

75

76

` cls.root.destroy()

`

76

77

` del cls.root

`

77

78

``

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

+

80

83

` can't invoke "event" command: application has been destroyed

`

81

84

` ...

`

82

85

` "ttk::ThemeChanged"

`

83

86

``

``

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

+

84

96

`Requires('gui') causes the test(s) it guards to be skipped if any of

`

85

97

`these conditions are met:

`

86

98

``