Skip to content

Commit 457bda8

Browse files
authored
Improve unit tests assertion messages (#2275)
Using `unittest` assertion methods where possible (since this project doesn't use pytest) Raise `NotImplementedError` for unimplemented methods Use `raise AssertionError` instead of `assert 0` (except in `pywin/debugger`, in case that's done on purpose)
1 parent 32002e2 commit 457bda8

File tree

17 files changed

+112
-110
lines changed

17 files changed

+112
-110
lines changed

Pythonwin/pywin/Demos/openGLDemo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ def _DestroyContexts(self):
160160

161161
# The methods to support OpenGL
162162
def DrawScene(self):
163-
assert 0, "You must override this method"
163+
raise NotImplementedError("You must override this method")
164164

165165
def Init(self):
166-
assert 0, "You must override this method"
166+
raise NotImplementedError("You must override this method")
167167

168168
def OnSizeChange(self, cx, cy):
169169
pass

Pythonwin/pywin/debugger/debugger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ def set_cur_frame(self, frame):
851851
self.curindex = index
852852
break
853853
else:
854-
assert 0, "Can't find the frame in the stack."
854+
assert False, "Can't find the frame in the stack."
855855
SetInteractiveContext(frame.f_globals, frame.f_locals)
856856
self.GUIRespondDebuggerData()
857857
self.ShowCurrentLine()
@@ -931,7 +931,7 @@ def GetDebuggerBar(self, barName):
931931
for id, klass, float in DebuggerDialogInfos:
932932
if klass.title == barName:
933933
return frame.GetControlBar(id)
934-
assert 0, "Can't find a bar of that name!"
934+
assert False, "Can't find a bar of that name!"
935935

936936
def GUIRespondDebuggerData(self):
937937
if not self.inited: # GUI not inited - no toolbars etc.

Pythonwin/pywin/framework/editor/template.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22

3-
import pywin.framework.window
43
import win32api
54
import win32ui
65
from pywin.mfc import docview
@@ -19,10 +18,10 @@ def __init__(
1918
ParentEditorTemplate.__init__(self, res, makeDoc, makeFrame, makeView)
2019

2120
def _CreateDocTemplate(self, resourceId):
22-
assert 0, "You must override this"
21+
raise NotImplementedError("You must override this")
2322

2423
def CreateWin32uiDocument(self):
25-
assert 0, "You must override this"
24+
raise NotImplementedError("You must override this")
2625

2726
def GetFileExtensions(self):
2827
return ".txt", ".py"

Pythonwin/pywin/idle/AutoIndent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def newline_and_indent_event(self, event):
262262
else:
263263
self.reindent_to(y.compute_backslash_indent())
264264
else:
265-
assert 0, "bogus continuation type " + repr(c)
265+
raise ValueError(f"bogus continuation type {c!r}")
266266
return "break"
267267

268268
# This line starts a brand new stmt; indent relative to

Pythonwin/pywin/test/test_exe.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def setUp(self):
4646
dst = os.path.dirname(pythonwinexe_path) + os.sep + pydll
4747
if not os.path.isfile(dst):
4848
try:
49-
assert os.path.isfile(src)
49+
self.assertTrue(os.path.isfile(src))
5050
print(f"-- symlink {dst!r} -> {src!r}", file=sys.stderr)
5151
os.symlink(src, dst)
5252
except (OSError, AssertionError) as e:
@@ -62,8 +62,8 @@ def test_exe(self):
6262
rc = "TIMEOUT"
6363
with open(self.tfn) as f:
6464
outs = f.read()
65-
assert rc == 0, f"rc is {rc!r}, outs={outs!r}"
66-
assert "Success!" in outs, outs
65+
self.assertEqual(rc, 0, f"outs={outs!r}")
66+
self.assertIn("Success!", outs)
6767
print("-- test_exe Ok! --", file=sys.stderr)
6868

6969
def tearDown(self):

0 commit comments

Comments
 (0)