lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.43k stars 186 forks source link

Option to disable output of script under test #543

Open ghost opened 7 years ago

ghost commented 7 years ago

I have a Lua module that prints to the console and I am not interested in seeing all of that when running unit tests. Is there an option in Busted to suppress all output the script under test produces?

Tieske commented 7 years ago

how about;

local old_print
setup(function()
  old_print = print
  print = function() end
end)
teardown(function()
  print = old_print
end
alerque commented 2 years ago

Alternatively write your module in such a way that the prints can be disabled. Busted cannot intercept your module's communication with the console. As already commented you can override print() and io.write() for the duration of the test.

I closed this as resolved, but actually it might be reasonable to have a (non-default) mode that captures output by automatically replacing print() and io.write() and caching the contents.

Tieske commented 2 years ago

this came up once, 5 years ago. Let's close, my 2cts.

alerque commented 2 years ago

I personally have several projects that could benefit from this as a feature. I've hacked around and just have lots of noisy output I try to ignore, the other I'm using a homegrown perl test runner to wrap the calls and catch the output even though it's a Lua project that could have used Busted directly. Hence why I was actually going to look at implementing this myself and re-opened. My 2¢.

Tieske commented 2 years ago

also needs overriding io.stderr:write and io.stdout:write and friends. While at the same time ensuring that the output handlers can actually write to those

viluon commented 1 year ago

This is a standard feature of test runners like Rust's cargo and would be very helpful. By default, print output could be disabled but tracked during the test run. It could be printed separately after the tests are done for each failed test, or optionally for all of them with a command line flag. That way, debugging failing tests would be easier without messing up Busted's own structured output.

musteresel commented 9 months ago

The suggestion from @Tieske didn't work for me - it did shut down any output done via print in the test file itself, but not from required files. This did the trick however:

local real_print = print
setup(function () _G.print = function (...) end end)
teardown(function() _G.print = real_print end)