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

assertNoError? #126

Closed eliasdaler closed 3 years ago

eliasdaler commented 4 years ago

Hello I think that having 'assertNoError' function would be useful. I know that I can just write no assertions and LuaUnit will say that there's an expected error when it surfaces, but it's still great to explicitly show in tests that certain things should not trigger an error.

bluebird75 commented 4 years ago

Sorry for the long answer. I don't see the real use case though. If a test is successful, there was no error. In which case would you use it specifically ?

eliasdaler commented 4 years ago

Well, for example Go has a popular testing library called testify. In it, we write tests like this:

f1, err := os.Open("some_file.txt")
assert.NoError(t, err, "file should exist")
f2, err := os.Open("missing.txt")
assert.Error(t, err, "file should not exist")

So, it's sometimes useful to explicitly say "this function call should not produce an error" and not just write a test without any assertions and commenting "if this test doesn't fail, it's successful".

bluebird75 commented 4 years ago

For me, there is a confusion in what you are saying. We have basically two types of error management: exceptions and return values.

If you are using a library with exception based error management, the exception will trigger in case of error, luaunit will catch it and report the test as failed. We are good here.

If you are using return values as error indication, then you need to check explicitely that they are no errors. There is no universal convention like in go on error management so you have to be explicit.

To draw from your go example, with luaunit, it would be :


lu = require('luaunit')

function test_1()
   f = io.open("some_file.txt")
   lu.assertNotNil(f)
   -- do something with f
end
eliasdaler commented 4 years ago

The return error values were a demonstration of how it NoError assertions worked in Go - I know that in Lua errors are exception-like.

To say in other terms, I want to write tests like this:

function test_ok_argument()
   lu.assertNoError(f, "ok_argument")
end

(in more complex cases, I can have a loop which iterates through a couple of "good" arguments to check that they work, but the function that gets tested might not return any values)

This just doesn't look like a proper test without any assertions:

function test_ok_argument()
   f("ok_argument") -- looks like a function call, not clear that we're testing that
   -- there's no error happening when called with this argument
end
bluebird75 commented 4 years ago

I don't see the interest of the proposition. the proposed assertNoError would just do a regular call without any added value. And you can already use the stack trace to know which line got the error.

I am going to close the request.