Skip to content

Commit 06aa907

Browse files
committed
Improve assertion logging
* Log all assertions, not just a few select ones. * Don't log assertion arguments because that would be too verbose. Instead, log only the file and the line where the assertion is checked. It should be enough for debugging. * Log where helpers.retrying is called. Also, fix formatting of fractional seconds. * Log test errors and failures so that one can figure out where the test failed by just looking at the unified log, without the luatest output. No changelog because the feature was added by commit d985997 ("Add more logs"), which hasn't been released yet.
1 parent 8a490e9 commit 06aa907

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

luatest/assertions.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local pp = require('luatest.pp')
1111
local log = require('luatest.log')
1212
local utils = require('luatest.utils')
1313
local tarantool = require('tarantool')
14+
local fio = require('fio')
1415
local ffi = require('ffi')
1516

1617
local prettystr = pp.tostring
@@ -271,7 +272,6 @@ end
271272
-- @string[opt] message
272273
-- @bool[opt] deep_analysis print diff.
273274
function M.assert_equals(actual, expected, message, deep_analysis)
274-
log.info('Assert %s equals to %s', actual, expected)
275275
if not comparator.equals(actual, expected) then
276276
failure(M.private.error_msg_equality(actual, expected, deep_analysis), message, 2)
277277
end
@@ -378,7 +378,6 @@ end
378378
-- @param expected
379379
-- @string[opt] message
380380
function M.assert_not_equals(actual, expected, message)
381-
log.info('Assert %s not equals to %s', actual, expected)
382381
if comparator.equals(actual, expected) then
383382
fail_fmt(2, message, 'Actual and expected values are equal: %s', prettystr(actual))
384383
end
@@ -502,8 +501,6 @@ M.private.str_match = str_match
502501
-- @bool[opt] is_pattern
503502
-- @string[opt] message
504503
function M.assert_str_contains(value, expected, is_pattern, message)
505-
log.info('Assert string %s contains %s', value, expected)
506-
507504
M.assert_type(value, 'string', nil, 2)
508505
M.assert_type(expected, 'string', nil, 2)
509506

@@ -646,7 +643,6 @@ end
646643
-- @param ... arguments for function
647644
function M.assert_error_msg_contains(expected_partial, fn, ...)
648645
local no_error, error_msg = pcall_wrapper(2, fn, ...)
649-
log.info('Assert error message %s contains %s', error_msg, expected_partial)
650646
if no_error then
651647
local failure_message = string.format(
652648
'Function successfully returned: %s\nExpected error containing: %s',
@@ -891,4 +887,16 @@ function M.assert_not_minus_zero(value, message)
891887
end
892888
end
893889

890+
-- Log all checked assertions for debugging.
891+
for func_name, func in pairs(M) do
892+
if func_name:startswith('assert') and type(func) == 'function' then
893+
M[func_name] = function(...)
894+
local info = debug.getinfo(2, 'Sl')
895+
log.info('Checking assertion at %s:%s',
896+
fio.basename(info.short_src), info.currentline)
897+
return func(...)
898+
end
899+
end
900+
end
901+
894902
return M

luatest/helpers.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
local clock = require('clock')
66
local fiber = require('fiber')
7+
local fio = require('fio')
78
local log = require('luatest.log')
89

910
local helpers = {}
@@ -69,7 +70,10 @@ function helpers.retrying(config, fn, ...)
6970
if (clock.time() - started_at) > timeout then
7071
return fn(...)
7172
end
72-
log.info('Retrying in %d sec due to error: %s', delay, result)
73+
local info = debug.getinfo(2, 'Sl')
74+
log.info('Retrying at %s:%s in %0.3f sec due to error:',
75+
fio.basename(info.short_src), info.currentline, delay)
76+
log.info(result)
7377
fiber.sleep(delay)
7478
end
7579
end

luatest/runner.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ end
548548

549549
function Runner.mt:invoke_test_function(test)
550550
local err = self:protected_call(test.group, test.method, test.name)
551+
if err.status == 'fail' or err.status == 'error' then
552+
log.error(err.message)
553+
log.error(err.trace)
554+
end
551555
self:update_status(test, err)
552556
end
553557

test/luatest_test.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ g.test_success_if_tnt_specific = function()
103103
t.assert_equals(helper.assert_failure(t.success_if, {}).status, 'success')
104104
end
105105

106-
g.test_assert_aliases = function ()
107-
t.assert_is(t.assert, t.assert_eval_to_true)
108-
t.assert_is(t.assert_not, t.assert_eval_to_false)
109-
end
110-
111106
g.test_assert_covers = function()
112107
local subject = t.assert_covers
113108
subject({a = 1, b = 2, c = 3}, {})

0 commit comments

Comments
 (0)