develooper Front page | | Postings from January 2005

Re: Test::Unit, ::Class, or ::Inline?

Thread Previous | Thread Next
David Golden
January 26, 2005 14:55
Re: Test::Unit, ::Class, or ::Inline?
Message ID:
Andy Lester wrote:
> I would love to hear your thoughts and ideas on structures of results.

Wild brainstorming here -- the issue seems to be with the fact that test 
success and failure is associated with a printed result.  What if the tests 
instead pushed a results object onto some global data stack?

Here's a quick conceptual example with three files (included at the end with 

   * -- provides "test_file" and "ok"
   * example.t -- a testfile using Tester
   * -- runs example.t and prints output

While this simple example just has the test function shove a reference to a 
hash onto a global array @{$Tester::RESULTS{$filename}}, the test function 
could just as easily create an object (Test::Object::Ok, Test::Object::Is, 
etc.) that holds relevant data for that type of test and push that onto the 
array.  E.g. for "is" it would hold the "got" and "expected" results.  Then 
there could be separate modules that interprets the array of objects and 
either prints them out as text with statistics, or prints them as HTML, etc. 
  Or the objects themselves could be required to stringify or htmlify, or 

Clearly, the devil is in the details and this is a really simple example, 
but if one is not adverse to playing with some globals, it seems doable. 
(Albeit requiring writing a complete alternative to Test::Harness...)

David Golden

### ###
package Tester;
use Exporter 'import';
our @EXPORT = qw( test_file ok );

%Tester::RESULTS = ();

sub test_file {
     my $tgt = shift;
     $Tester::RESULTS{$tgt} = [];
     do "$tgt";
     return @{$Tester::RESULTS{$tgt}};

sub ok {
     my ($val,$name) = @_;
     my ($package, $filename, $line) = caller;
     my $result = $val ? 1 : 0;
     my $hr = {
         filename => $filename,
         line => $line,
         result => $result,
         name => $name,
         data => $val
     push @{$Tester::RESULTS{$filename}}, $hr;


### ###
use warnings;
use strict;

use Tester;
use YAML;

my @results = test_file("example.t");
print Dump(@results);

### example.t ###
use lib ".";
use Tester;

ok( 3, "test #1" );
ok( 1 == 1, "test #2" );

### Output ###
--- #YAML:1.0
data: 3
filename: example.t
line: 4
name: test #1
result: 1
--- #YAML:1.0
data: 1
filename: example.t
line: 5
name: test #2
result: 1

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About