Is there reliable approach to capture NIF's stdout and stderr outputs

ExUnit.CaptureIO could only capture output by IO.puts or IO.inspect but if the output is by NIF the capture_io returns an empty string.

Not really because IO operations in NIFs do not necessarily go through the runtime system.

You could change the NIFs to either:

  1. Send a message to the relevant IO process (aka the group leader) from the NIF (you can pass the group_leader when calling the NIF and message it)

  2. You send them as messages to another process which will either log or call the relevant IO module

This makes the system asynchronous though, which may on the other hand complicate the testing of the captured contents.

1 Like