Skip to content

Commit 3e8d7a1

Browse files
authored
Add tests utility (#849)
* Print caller position when assertion fails in an auxiliary file
1 parent dddc7d0 commit 3e8d7a1

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tests/flow/includes.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
MAX_TRANSACTIONS=100
3333

3434

35+
# returns the test name and line number from which a helper function within this file was called.
36+
# For example, if an assertion fails in check_error_message function, and the caller function to check_error_message
37+
# is in tests_onnx.py line 25, this should return: "tests_onnx:py:25"
38+
def get_caller_pos():
39+
return f'{sys._getframe(2).f_code.co_filename.split("/")[-1]}:{sys._getframe(2).f_lineno}'
40+
3541
def ensureSlaveSynced(con, env, timeout_ms=0):
3642
if env.useSlaves:
3743
# When WAIT returns, all the previous write commands
@@ -43,7 +49,7 @@ def ensureSlaveSynced(con, env, timeout_ms=0):
4349
except Exception as ex:
4450
# Error in converting to int
4551
env.debugPring(str(ex), force=True)
46-
env.assertFalse(True)
52+
env.assertFalse(True, message=get_caller_pos())
4753
return
4854
env.assertEqual(number_replicas, 1)
4955

@@ -220,23 +226,23 @@ def load_file_content(file_name):
220226
def check_error_message(env, con, error_msg, *command, error_msg_is_substr=False):
221227
try:
222228
con.execute_command(*command)
223-
env.assertFalse(True)
229+
env.assertFalse(True, message=get_caller_pos())
224230
except Exception as exception:
225-
env.assertEqual(type(exception), redis.exceptions.ResponseError)
231+
env.assertEqual(type(exception), redis.exceptions.ResponseError, message=get_caller_pos())
226232
if error_msg_is_substr:
227233
# We only verify that the given error_msg is a substring of the entire error message.
228-
env.assertTrue(str(exception).find(error_msg) > 0)
234+
env.assertTrue(str(exception).find(error_msg) >= 0, message=get_caller_pos())
229235
else:
230-
env.assertEqual(error_msg, str(exception))
236+
env.assertEqual(error_msg, str(exception), message=get_caller_pos())
231237

232238

233239
def check_error(env, con, *command):
234240
try:
235241
con.execute_command(*command)
236-
env.assertFalse(True)
242+
env.assertFalse(True, message=get_caller_pos())
237243
except Exception as e:
238244
exception = e
239-
env.assertEqual(type(exception), redis.exceptions.ResponseError)
245+
env.assertEqual(type(exception), redis.exceptions.ResponseError, message=get_caller_pos())
240246

241247

242248
# Returns a dict with all the fields of a certain section from INFO MODULES command

0 commit comments

Comments
 (0)