Closed ymh199478 closed 4 years ago
Do you mean the stack trace displayed along with the error ? It would help me understand better if you can provide an example of what you would like to achieve and what is the current result.
I am wrapped the luaunit
similar to the following:
local Assert = require("luaunit/luaunit")
local TestCase = {}
function TestCase:assertEquals(expected, actual, message)
Assert.assertEquals(actual, expected, message)
end
If this is used by developers, an exception will cause the following output:
./src/whale/foundation/testing/test-case.lua:15: expected: 1, actual: 2
stack traceback:
./src/whale/foundation/testing/test-case.lua:15: in function 'whale/foundation/testing/test-case.assertEquals'
./tests/foundation/application-test.lua:45: in function 'tests/foundation/application-test.testBootingCallbacks'
but, my expected output should be:
./tests/foundation/application-test.lua:45: expected: 1, actual: 2
stack traceback:
./tests/foundation/application-test.lua:45: in function 'tests/foundation/application-test.testBootingCallbacks'
I am developing a framework project, so I need to wrapped luaunit
. Instead of letting developer directly use luaunit, it is not conducive to future expansion.
I have not found a better solution in luaunit that allows me to adjust the error level
.
Thanks that's clear now. I have two ideas to address it, I'll evaluate them and tell you. The ideas are either to strip explicitely the lines you don't want from the stack trace or to adjust the error level.
For stripping the stack trace, you can modify the function stripLuaunitTrace() (around line 338 in luaunit.lua ). The other path is to play with the error level failure(), fail_fmt() and error_fmt().
Ok, after a bit of experimenting, you will have to do both options described :
- error(M.FAILURE_PREFIX .. msg, (level or 1) + 1)
+ error(M.FAILURE_PREFIX .. msg, (level or 1) + 2)
The effect is that the first line reported of where the error occured, before the stack trace will no longer reference your internal framework. This should yield something like :
./tests/foundation/application-test.lua:45: expected: 1, actual: 2
stack traceback:
./src/whale/foundation/testing/test-case.lua:15: in function 'whale/foundation/testing/test-case.assertEquals'
./tests/foundation/application-test.lua:45: in function 'tests/foundation/application-test.testBootingCallbacks'
By the way, it's better to use the latest code (of today) for this. There were a few minor functions which would not go through failure_fmt() or error_fmt()
That's an interesting need by the way. I'll add official support for this in the next release of luaunit.
Thanks, Direct modification of the luaunit code will make future updates difficult, so i am looking forward to seeing this feature in the next version. ^_^
Hi. This ability is now directly included in luaunit. If you use the latest version, look for the variable STRIP_EXTRA_ENTRIES_IN_STACK_TRACE
and set it to 1 to get the desired behavior.
Awesome, thanks.
I am developing a lua framework, and I chose to use luaunit when designing unit testing schemes.
I need to wrapped luaunit, but this will break the hierarchy displayed when the assertion is raised.
Can I have a way to modify the level of
error