1Test::Strict(3) User Contributed Perl Documentation Test::Strict(3)
2
3
4
6 Test::Strict - Check syntax, presence of use strict; and test coverage
7
9 "Test::Strict" lets you check the syntax, presence of "use strict;" and
10 presence "use warnings;" in your perl code. It report its results in
11 standard "Test::Simple" fashion:
12
13 use Test::Strict tests => 3;
14 syntax_ok( 'bin/myscript.pl' );
15 strict_ok( 'My::Module', "use strict; in My::Module" );
16 warnings_ok( 'lib/My/Module.pm' );
17
18 Module authors can include the following in a t/strict.t and have
19 "Test::Strict" automatically find and check all perl files in a module
20 distribution:
21
22 use Test::Strict;
23 all_perl_files_ok(); # Syntax ok and use strict;
24
25 or
26
27 use Test::Strict;
28 all_perl_files_ok( @mydirs );
29
30 "Test::Strict" can also enforce a minimum test coverage the test suite
31 should reach. Module authors can include the following in a t/cover.t
32 and have "Test::Strict" automatically check the test coverage:
33
34 use Test::Strict;
35 all_cover_ok( 80 ); # at least 80% coverage
36
37 or
38
39 use Test::Strict;
40 all_cover_ok( 80, 't/' );
41
43 The most basic test one can write is "does it compile ?". This module
44 tests if the code compiles and play nice with "Test::Simple" modules.
45
46 Another good practice this module can test is to "use strict;" in all
47 perl files.
48
49 By setting a minimum test coverage through "all_cover_ok()", a code
50 author can ensure his code is tested above a preset level of kwality
51 throughout the development cycle.
52
53 Along with Test::Pod, this module can provide the first tests to setup
54 for a module author.
55
56 This module should be able to run under the -T flag for perl >= 5.6.
57 All paths are untainted with the following pattern:
58 "qr|^([-+@\w./:\\]+)$|" controlled by $Test::Strict::UNTAINT_PATTERN.
59
61 syntax_ok( $file [, $text] )
62 Run a syntax check on $file by running "perl -c $file" with an external
63 perl interpreter. The external perl interpreter path is stored in
64 $Test::Strict::PERL which can be modified. You may prefer "use_ok()"
65 from Test::More to syntax test a module. For a module, the path
66 (lib/My/Module.pm) or the name (My::Module) can be both used.
67
68 strict_ok( $file [, $text] )
69 Check if $file contains a "use strict;" statement.
70
71 This is a pretty naive test which may be fooled in some edge cases.
72 For a module, the path (lib/My/Module.pm) or the name (My::Module) can
73 be both used.
74
75 warnings_ok( $file [, $text] )
76 Check if warnings have been turned on.
77
78 If $file is a module, check if it contains a "use warnings;" or "use
79 warnings::..." statement. However, if the perl version is <= 5.6, this
80 test is skipped ("use warnings" appeared in perl 5.6).
81
82 If $file is a script, check if it starts with "#!...perl -w". If the
83 -w is not found and perl is >= 5.6, check for a "use warnings;" or "use
84 warnings::..." statement.
85
86 This is a pretty naive test which may be fooled in some edge cases.
87 For a module, the path (lib/My/Module.pm) or the name (My::Module) can
88 be both used.
89
90 all_perl_files_ok( [ @directories ] )
91 Applies "strict_ok()" and "syntax_ok()" to all perl files found in
92 @directories (and sub directories). If no <@directories> is given, the
93 starting point is one level above the current running script, that
94 should cover all the files of a typical CPAN distribution. A perl file
95 is *.pl or *.pm or *.t or a file starting with "#!...perl"
96
97 If the test plan is defined:
98
99 use Test::Strict tests => 18;
100 all_perl_files_ok();
101
102 the total number of files tested must be specified.
103
104 You can control which tests are run on each perl site through:
105
106 $Test::Strict::TEST_SYNTAX (default = 1)
107 $Test::Strict::TEST_STRICT (default = 1)
108 $Test::Strict::TEST_WARNINGS (default = 0)
109 $Test::Strict::TEST_SKIP (default = []) "Trusted" files to skip
110
111 all_cover_ok( [coverage_threshold [, @t_dirs]] )
112 This will run all the tests in @t_dirs (or current script's directory
113 if @t_dirs is undef) under Devel::Cover and calculate the global test
114 coverage of the code loaded by the tests. If the test coverage is
115 greater or equal than "coverage_threshold", it is a pass, otherwise
116 it's a fail. The default coverage threshold is 50 (meaning 50% of the
117 code loaded has been covered by test).
118
119 The threshold can be modified through
120 $Test::Strict::COVERAGE_THRESHOLD.
121
122 You may want to select which files are selected for code coverage
123 through $Test::Strict::DEVEL_COVER_OPTIONS, see Devel::Cover for the
124 list of available options. The default is '+ignore,"/Test/Strict\b"'.
125
126 The path to "cover" utility can be modified through
127 $Test::Strict::COVER.
128
129 The 50% threshold is a completely arbitrary value, which should not be
130 considered as a good enough coverage.
131
132 The total coverage is the return value of "all_cover_ok()".
133
135 For "all_cover_ok()" to work properly, it is strongly advised to
136 install the most recent version of Devel::Cover and use perl 5.8.1 or
137 above. In the case of a "make test" scenario, "all_perl_files_ok()"
138 re-run all the tests in a separate perl interpreter, this may lead to
139 some side effects.
140
142 Test::More, Test::Pod. Test::Distribution, <Test:NoWarnings>
143
145 Pierre Denis, "<pierre@itrelease.net>".
146
148 Copyright 2005, Pierre Denis, All Rights Reserved.
149
150 You may use, modify, and distribute this package under the same terms
151 as Perl itself.
152
153
154
155perl v5.12.0 2009-01-31 Test::Strict(3)