Skip to content

Commit

Permalink
Improve unit tests assertion messages (#2275)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Avasam committed Jun 4, 2024
1 parent 32002e2 commit 457bda8
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 110 deletions.
4 changes: 2 additions & 2 deletions Pythonwin/pywin/Demos/openGLDemo.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ def _DestroyContexts(self):

# The methods to support OpenGL
def DrawScene(self):
assert 0, "You must override this method"
raise NotImplementedError("You must override this method")

def Init(self):
assert 0, "You must override this method"
raise NotImplementedError("You must override this method")

def OnSizeChange(self, cx, cy):
pass
Expand Down
4 changes: 2 additions & 2 deletions Pythonwin/pywin/debugger/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ def set_cur_frame(self, frame):
self.curindex = index
break
else:
assert 0, "Can't find the frame in the stack."
assert False, "Can't find the frame in the stack."
SetInteractiveContext(frame.f_globals, frame.f_locals)
self.GUIRespondDebuggerData()
self.ShowCurrentLine()
Expand Down Expand Up @@ -931,7 +931,7 @@ def GetDebuggerBar(self, barName):
for id, klass, float in DebuggerDialogInfos:
if klass.title == barName:
return frame.GetControlBar(id)
assert 0, "Can't find a bar of that name!"
assert False, "Can't find a bar of that name!"

def GUIRespondDebuggerData(self):
if not self.inited: # GUI not inited - no toolbars etc.
Expand Down
5 changes: 2 additions & 3 deletions Pythonwin/pywin/framework/editor/template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

import pywin.framework.window
import win32api
import win32ui
from pywin.mfc import docview
Expand All @@ -19,10 +18,10 @@ def __init__(
ParentEditorTemplate.__init__(self, res, makeDoc, makeFrame, makeView)

def _CreateDocTemplate(self, resourceId):
assert 0, "You must override this"
raise NotImplementedError("You must override this")

def CreateWin32uiDocument(self):
assert 0, "You must override this"
raise NotImplementedError("You must override this")

def GetFileExtensions(self):
return ".txt", ".py"
Expand Down
2 changes: 1 addition & 1 deletion Pythonwin/pywin/idle/AutoIndent.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def newline_and_indent_event(self, event):
else:
self.reindent_to(y.compute_backslash_indent())
else:
assert 0, "bogus continuation type " + repr(c)
raise ValueError(f"bogus continuation type {c!r}")
return "break"

# This line starts a brand new stmt; indent relative to
Expand Down
6 changes: 3 additions & 3 deletions Pythonwin/pywin/test/test_exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def setUp(self):
dst = os.path.dirname(pythonwinexe_path) + os.sep + pydll
if not os.path.isfile(dst):
try:
assert os.path.isfile(src)
self.assertTrue(os.path.isfile(src))
print(f"-- symlink {dst!r} -> {src!r}", file=sys.stderr)
os.symlink(src, dst)
except (OSError, AssertionError) as e:
Expand All @@ -62,8 +62,8 @@ def test_exe(self):
rc = "TIMEOUT"
with open(self.tfn) as f:
outs = f.read()
assert rc == 0, f"rc is {rc!r}, outs={outs!r}"
assert "Success!" in outs, outs
self.assertEqual(rc, 0, f"outs={outs!r}")
self.assertIn("Success!", outs)
print("-- test_exe Ok! --", file=sys.stderr)

def tearDown(self):
Expand Down

0 comments on commit 457bda8

Please sign in to comment.