Test::Count - Module for keeping track of the number of tests in a test script.
$ cat "t/mytest.t" | perl -MTest::Count::Filter -e 'filter()'
Test::Count is a set of perl modules for keeping track of the number of tests in a test file. It works by putting in comments of the form # TEST (one test), # TEST*$EXPR or # TEST+$EXPR (both are multiple tests). Test::Count count these tests throughout the fileand return all of their results.
# TEST
# TEST*$EXPR
# TEST+$EXPR
One can put any mathematical expressions (using parentheses, +, -, *, / and % there). One can also assign variables using # TEST:$myvar=5+6;$second_var=$myvar+3 and later use them in the add to count expressions. A $var++ construct is also available.
+
-
*
/
%
# TEST:$myvar=5+6;$second_var=$myvar+3
$var++
One can find example test scripts under t/.
A simple Vim (http://www.vim.org/) function to update the count of the tests in the file is:
function! Perl_Tests_Count() %!perl -MTest::Count::Filter -e 'Test::Count::Filter->new({})->process()' endfunction
Creates a new Test::Count object that process the filehandle specified in 'input_fh'. Optional keys are:
'input_fh'
'assert_prefix_regex' => qr{; TEST}
A regular expression for specifying the prefix for a "TEST" assertion that updates the grammar. Defaults to "# TEST".
"# TEST"
Process the filehandle specified in 'input_fh' in ->new(), and return a hash ref with the following keys:
tests_count
The count of the test.
lines
The lines of the stream as is.
You can put any mathematical expressions (using parentheses, +, -, *, / and % there). You can also assign variables using # TEST:$myvar=5+6;$second_var=$myvar+3 and later use them in the add to count expressions. A $var++ construct is also available. Also available are +=, -= and *=.
+=
-=
*=
You can also do # TEST:source "path-to-file-here.txt" where the filename comes in quotes, in order to include the filename and process it (similar to the C-shell or Bash "source" command) . You can use the special variable $^CURRENT_DIRNAME there for the dirname of the current file.
# TEST:source "path-to-file-here.txt"
$^CURRENT_DIRNAME
Finally, # TEST*EXPR() and # TEST+$EXPR() add tests to the count.
# TEST*EXPR()
# TEST+$EXPR()
The first example is very trivial:
#!/usr/bin/perl use strict; use warnings; use Test::More tests => 2; # TEST ok (1, "True is true."); { my $val = 'foobar'; # TEST is ($val, 'foobar', 'The variable $val has the right value.'); }
As you can see, the # TEST comments are very close to the assertions where they are easily noticable and easy to maintain by the tests (if more tests are added or removed).
Now, let's suppose you have several files which you'd like to make sure validate according to the spec, and are processed well using the processor.
#!/usr/bin/perl use strict; use warnings; use Test::More tests => 18; use IO::All; use Test::Differences; use MyFormatProcessor; # TEST:$num_files=6; my @basenames = (qw( basic with_ampersands with_comments with_bold with_italics with_bold_and_italics )); foreach my $basename (@basenames) { my $processor = MyFormatProcessor->new( { filename => "t/data/input/$basename.myformat", } ); # TEST*$num_files ok ($processor, "Construction of a processor for '$basename' was successful." ); # TEST*$num_files ok (scalar($processor->is_valid()), "'$basename' is valid."); # TEST*$num_files eq_or_diff ($processor->convert_to_xhtml, scalar(io("t/data/want-output/$basename.xhtml")->slurp()), "Converting '$basename' is successful." ); }
As you can see, the number of files is kept in one central place, and each assertion inside the loop is multiplied by it. So if we add or remove files, we only need to add or remove them from their declarations.
Shlomi Fish, http://www.shlomifish.org/ .
Please report any bugs or feature requests to bug-test-count at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test::Count. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-test-count at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Test::Count
You can also look for information at:
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Test::Count
CPAN Ratings
http://cpanratings.perl.org/d/Test::Count
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test::Count
Search CPAN
http://search.cpan.org/dist/Test::Count
Copyright 2006 Shlomi Fish.
This program is released under the following license: MIT X11.
To install Test::Count, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Count
CPAN shell
perl -MCPAN -e shell install Test::Count
For more information on module installation, please visit the detailed CPAN module installation guide.