Test-More / test-more

Test2, Test::More, Test::Simple and Test::Builder Perl modules for writing tests
Other
140 stars 89 forks source link

Redirect output #958

Open sidey79 opened 4 years ago

sidey79 commented 4 years ago

I'm trying to migrate to Test2 suite but there ist something not clear for me.

With Test::Builder it was possible to write output into a variable and use this in the program: https://perldoc.perl.org/Test/Builder.html#Output

How can i capture the output with test2 suite?

exodist commented 4 years ago

It would help if you could tell me why you want to capture the output. Test2 has several capture mechanisms, and each has a different use case. I need to know what you are trying to accomplish by capturing.

For starters there is: my $events = intercept { ... } which lets you grab all events from the codeblock, but in their object form.

You can also use a custom formatter (See Test2::Formatter::TAP for the default formatter, and Test2::Formatter::Test2 for a pretty one)

The Test2::Formatter::TAP formatter also lets you replace the filehandles it uses the same way Test::Builder does if you just want to intercept the TAP stream, but honestly for most things there are better options now.

sidey79 commented 4 years ago

Thanks for your quick response.

I want to test modules which are written inside an application. Because i don't want to simulate the application i wrote a test module for this application.

The only thing i want to archive is, capuring the test results to provide them inside the application.

sidey79 commented 4 years ago

@exodist

In the meantime i tried to capture the output, but i didn't got it working.

May you have some hints for me, in what direction i should look.

exodist commented 4 years ago

Normally for intercepting test events you would use intercept(): use Test2::API qw/intercept/; my $events = intercept { ok(1, "Pass"); ok(0, "Fail"); done_testing();

However that grabs Test2 events, not "output". It also prevents the tests from counting globally.

Let me know if that is what you needed!

sidey79 commented 4 years ago

Thanks for your replay. I testest a little bit with intercept and saw that there are the events noted.

I also tried grab which dies nearly the same. On one side, i can work with this information, but my plan was to capture the output of the Tap formatter and provide this inside the application

Should i implement a additional formatter or is it possible to capture the output from the formatter?

exodist commented 4 years ago

You should probably subclass one of the tap formatters and have it store/send/etc the output instead of printing it. The way Test::Builder and Test2 were designed are for the formatters or tap to be read by another process as a harness. Your use case is somewhat unconventional, but a subclasses TAP formatter can do what you want.