1
+ import time
1
2
import unittest
2
3
3
4
import comtypes
4
5
import comtypes .client
5
6
import comtypes .test
6
7
7
- comtypes .test .requires ("ui" )
8
+ try :
9
+ # pass Word libUUID
10
+ comtypes .client .GetModule (("{00020905-0000-0000-C000-000000000046}" ,))
11
+ IMPORT_FAILED = False
12
+ except (ImportError , OSError ):
13
+ IMPORT_FAILED = True
8
14
9
15
10
- def setUpModule ():
11
- raise unittest .SkipTest (
12
- "External test dependencies like this seem bad. Find a different "
13
- "built-in win32 API to use."
14
- )
16
+ ################################################################
17
+ #
18
+ # TODO:
19
+ #
20
+ # It seems bad that only external test like this
21
+ # can verify the behavior of `comtypes` implementation.
22
+ # Find a different built-in win32 API to use.
23
+ #
24
+ ################################################################
15
25
16
26
27
+ @unittest .skipIf (IMPORT_FAILED , "This depends on Word." )
17
28
class Test (unittest .TestCase ):
18
- def tearDown (self ):
19
- if hasattr (self , "w1" ):
20
- self .w1 .Quit ()
21
- del self .w1
22
-
23
- def test (self ):
29
+ def setUp (self ):
24
30
try :
25
31
comtypes .client .GetActiveObject ("Word.Application" )
26
32
except WindowsError :
27
33
pass
28
34
else :
29
35
# seems word is running, we cannot test this.
30
36
self .fail ("MSWord is running, cannot test" )
31
-
32
37
# create a WORD instance
33
- self .w1 = w1 = comtypes .client .CreateObject ("Word.Application" )
38
+ self .w1 = comtypes .client .CreateObject ("Word.Application" )
39
+
40
+ def tearDown (self ):
41
+ if hasattr (self , "w1" ):
42
+ self .w1 .Quit ()
43
+ del self .w1
44
+
45
+ def test (self ):
34
46
# connect to the running instance
47
+ w1 = self .w1
35
48
w2 = comtypes .client .GetActiveObject ("Word.Application" )
36
49
37
50
# check if they are referring to the same object
@@ -42,21 +55,16 @@ def test(self):
42
55
w1 .Quit ()
43
56
del self .w1
44
57
45
- import time
46
-
47
58
time .sleep (1 )
48
59
49
- try :
60
+ with self . assertRaises ( comtypes . COMError ) as arc :
50
61
w2 .Visible
51
- except comtypes .COMError as err :
52
- variables = err .hresult , err .text , err .details
53
- self .assertEqual (variables , err [:])
54
- else :
55
- raise AssertionError ("COMError not raised" )
56
62
57
- self .assertRaises (
58
- WindowsError , comtypes .client .GetActiveObject , "Word.Application"
59
- )
63
+ err = arc .exception
64
+ variables = err .hresult , err .text , err .details
65
+ self .assertEqual (variables , err .args )
66
+ with self .assertRaises (WindowsError ):
67
+ comtypes .client .GetActiveObject ("Word.Application" )
60
68
61
69
62
70
if __name__ == "__main__" :
0 commit comments