Search code examples
fantom

Redirect standard output and standard err when executing a method


I have a program that tests each method in a Test# subclass and outputs XML in JUnit's XML format.

For instance:

class ExampleTest : Test
{
  Void testOne()
  {
   ... 
  }
}

I want to execute the testOne method and capture the standard output and standard error produced in it. This out and err output will be included in the XML report.

My first idea was to look at sys::Env. The environment class sys::Env has err and out but are readonly.

My second idea is that sys::Process can be launched for each test method and redirect sys::Process#.err and sys::Process#.out in it, but I'm afraid it will be very slow.

There is other way to do it?


Solution

  • You won't be able to redirect output from your current process (and really should not).

    If the output absolutely has to be stdout/err - you'll need to go the Process route. You'll take the fork/jvm/stream setup hit, but that may be negligible compared to your test runtime.

    A better option would be to log using the Logging API - which will give more control over what gets logged, and where things go.