Skip to content

Commit 00f2317

Browse files
authored
remove raising unittest.SkipTest from test_getactiveobj (#462)
1 parent 95962e9 commit 00f2317

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

comtypes/test/test_getactiveobj.py

+33-25
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
1+
import time
12
import unittest
23

34
import comtypes
45
import comtypes.client
56
import comtypes.test
67

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
814

915

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+
################################################################
1525

1626

27+
@unittest.skipIf(IMPORT_FAILED, "This depends on Word.")
1728
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):
2430
try:
2531
comtypes.client.GetActiveObject("Word.Application")
2632
except WindowsError:
2733
pass
2834
else:
2935
# seems word is running, we cannot test this.
3036
self.fail("MSWord is running, cannot test")
31-
3237
# 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):
3446
# connect to the running instance
47+
w1 = self.w1
3548
w2 = comtypes.client.GetActiveObject("Word.Application")
3649

3750
# check if they are referring to the same object
@@ -42,21 +55,16 @@ def test(self):
4255
w1.Quit()
4356
del self.w1
4457

45-
import time
46-
4758
time.sleep(1)
4859

49-
try:
60+
with self.assertRaises(comtypes.COMError) as arc:
5061
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")
5662

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")
6068

6169

6270
if __name__ == "__main__":

0 commit comments

Comments
 (0)