-
Notifications
You must be signed in to change notification settings - Fork 12
Description
Luatest prints the stack trace if the test fails an assertion or raises an error. This is really useful in case a failure happens in a function, which is called in more than one place in the test, because in this case the assertion/error itself will point to the code inside the function so that without the stack trace one can't figure out where the test failed.
To illustrate this issue, here's a test.
test file
local t = require('luatest')
local server = require('luatest.server')
local g = t.group()
g.before_all(function(cg)
cg.server = server:new()
cg.server:start()
end)
g.after_all(function(cg)
cg.server:drop()
end)
g.test_fail_local = function()
local function check(x)
t.assert_ge(x, 1)
t.assert_le(x, 5)
end
check(1)
check(10)
end
g.test_fail_server = function(cg)
cg.server:exec(function()
local function check(x)
t.assert_ge(x, 1)
t.assert_le(x, 5)
end
check(1)
check(10)
end)
end
g.test_error_local = function()
local function check(x)
if x < 1 or x > 5 then
error('invalid x', 0)
end
end
check(1)
check(10)
end
g.test_error_server = function(cg)
cg.server:exec(function()
local function check(x)
if x < 1 or x > 5 then
error('invalid x', 0)
end
end
check(1)
check(10)
end)
endPut this test somewhere in the Tarantool repository and run. You'll get the following output.
luatest output
Tests with errors:
------------------
1) box-luatest.test.test_error_local
invalid x
stack traceback:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:38: in function 'check'
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:42: in function 'box-luatest.test.test_error_local'
...
[C]: in function 'xpcall'
2) box-luatest.test.test_error_server
invalid x
stack traceback:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:46: in function 'box-luatest.test.test_error_server'
...
[C]: in function 'xpcall'
artifacts:
server -> /tmp/t/artifacts/server-cU_eIc40j6IC
Failed tests:
-------------
1) box-luatest.test.test_fail_local
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:18: Assertion failed: 10 <= 5
stack traceback:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:18: in function 'check'
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:21: in function 'box-luatest.test.test_fail_local'
...
[C]: in function 'xpcall'
2) box-luatest.test.test_fail_server
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:28: Assertion failed: 10 <= 5
stack traceback:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:25: in function 'box-luatest.test.test_fail_server'
...
[C]: in function 'xpcall'
artifacts:
server -> /tmp/t/artifacts/server-cU_eIc40j6IC
Note that when the check() function is called locally the stack trace can be used to pinpoint the call location:
stack traceback:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:18: in function 'check'
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:21: in function 'box-luatest.test.test_fail_local'
stack traceback:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:18: in function 'check'
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:21: in function 'box-luatest.test.test_fail_local'
If check() is called from server:exec(), the stack trace points to the line where server:exec() is located:
stack traceback:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:46: in function 'box-luatest.test.test_error_server'
stack traceback:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:25: in function 'box-luatest.test.test_fail_server'
If the test fails an assertion, we can at least see the line where the assertion is located because it seems to be attached to the assertion error object. This isn't enough in our case but it's something:
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:18: Assertion failed: 10 <= 5
...d/src/tarantool/tarantool/test/box-luatest/test_test.lua:28: Assertion failed: 10 <= 5
If the test fails with an error, we lack even this little information (fat least for raw Lua errors; Box errors are enriched with file:line but they don't always point where they should).