bluebird75 / luaunit

LuaUnit is a popular unit-testing framework for Lua, with an interface typical of xUnit libraries (Python unittest, Junit, NUnit, ...). It supports several output formats (Text, TAP, JUnit, ...) to be used directly or work with Continuous Integration platforms (Jenkins, Maven, ...).
Other
572 stars 137 forks source link

assertError does not work? #141

Closed Ismoh closed 2 years ago

Ismoh commented 2 years ago

Hey, I am new to Lua and LuaUnit. I am not sure, if I do anything wrong, but the assertion of errors does not work for me.

The error itself:

error("Unix system are not supported yet :(",2)

https://github.com/Ismoh/NoitaMP/blob/b52f1b7e0e2f50df53d90eda569f055616201df7/noita-mp/files/scripts/util/file_util.lua#L99

The test file and functions:

function TestFileUtil:testGetRelativeDirectoryAndFilesOfSave06()
    lu.assertError(fu.GetRelativeDirectoryAndFilesOfSave06())
    lu.assertErrorMsgContains("Unix system are not supported yet", fu.GetRelativeDirectoryAndFilesOfSave06())
end

https://github.com/Ismoh/NoitaMP/blob/b52f1b7e0e2f50df53d90eda569f055616201df7/.testing/tests/file_util_test.lua#L65-L68

The test output:

file_util.lua | Detected Unix with path separator '/'.
 Started on Sun Dec 19 15:40:36 2021
     TestFileUtil.testGetRelativeDirectoryAndFilesOfSave06 ... 

 ERROR
 ...r/work/NoitaMP/NoitaMP/.testing/tests/file_util_test.lua:66: Unix system are not supported yet :(

 ERROR during LuaUnit test execution:
 ...r/work/NoitaMP/NoitaMP/.testing/tests/file_util_test.lua:66: Unix system are not supported yet :(
 =========================================================
 Tests with errors:
 ------------------
 1) TestFileUtil.testGetRelativeDirectoryAndFilesOfSave06
 ...r/work/NoitaMP/NoitaMP/.testing/tests/file_util_test.lua:66: Unix system are not supported yet :(
stack traceback:
    ...r/work/NoitaMP/NoitaMP/.testing/tests/file_util_test.lua:66: in function 'TestFileUtil.testGetRelativeDirectoryAndFilesOfSave06'

Ran 1 tests in 0.000 seconds, 0 successes, 1 error
LuaUnit ABORTED (as requested by --error or --failure option)
ERROR during LuaUnit test execution:

I expected the test to succeed with "Ok". Am I wrong?

bluebird75 commented 2 years ago

In the code shown, you are doing directly the call to fu.GetRelativeDirectoryAndFilesOfSave06() and get the error immediatly. To work with luaunit, you must not do the call yourself, but just pass the function to call. LuaUnit will call it in an environment where it can catch the error.

So; this should be :

function TestFileUtil:testGetRelativeDirectoryAndFilesOfSave06()
    lu.assertError(fu.GetRelativeDirectoryAndFilesOfSave06)
    lu.assertErrorMsgContains("Unix system are not supported yet", fu.GetRelativeDirectoryAndFilesOfSave06)
end
Ismoh commented 2 years ago

Ah, this makes completly sense 😆 Thanks for your fast reply and help! I will change it tomorrow. Can be closed! 😎