1Test::NoWarnings(3) User Contributed Perl Documentation Test::NoWarnings(3)
2
3
4
6 Test::NoWarnings - Make sure you didn't emit any warnings while testing
7
9 For scripts that have no plan
10
11 use Test::NoWarnings;
12
13 that's it, you don't need to do anything else
14
15 For scripts that look like
16
17 use Test::More tests => x;
18
19 change to
20
21 use Test::More tests => x + 1;
22 use Test::NoWarnings;
23
25 In general, your tests shouldn't produce warnings. This modules causes
26 any warnings to be captured and stored. It automatically adds an extra
27 test that will run when your script ends to check that there were no
28 warnings. If there were any warings, the test will give a "not ok" and
29 diagnostics of where, when and what the warning was, including a stack
30 trace of what was going on when the it occurred.
31
32 If some of your tests are supposed to produce warnings then you should
33 be capturing and checking them with Test::Warn, that way
34 Test::NoWarnings will not see them and so not complain.
35
36 The test is run by an "END" block in Test::NoWarnings. It will not be
37 run when any forked children exit.
38
40 Simply by using the module, you automatically get an extra test at the
41 end of your script that checks that no warnings were emitted. So just
42 stick
43
44 use Test::NoWarnings;
45
46 at the top of your script and continue as normal.
47
48 If you want more control you can invoke the test manually at any time
49 with "had_no_warnings".
50
51 The warnings your test has generated so far are stored in an array. You
52 can look inside and clear this whenever you want with "warnings()" and
53 "clear_warnings", however, if you are doing this sort of thing then you
54 probably want to use Test::Warn in combination with Test::NoWarnings.
55
56 use vs require
57 You will almost always want to do
58
59 use Test::NoWarnings
60
61 If you do a "require" rather than a "use", then there will be no
62 automatic test at the end of your script.
63
64 Output
65 If warning is captured during your test then the details will output as
66 part of the diagnostics. You will get:
67
68 o the number and name of the test that was executed just before the
69 warning (if no test had been executed these will be 0 and '')
70
71 o the message passed to "warn",
72
73 o a full dump of the stack when warn was called, courtesy of the "Carp"
74 module
75
76 By default, all warning messages will be emitted in one block at the
77 end of your test script.
78
79 The :early pragma
80 One common complaint from people using Test::NoWarnings is that all of
81 the warnings are emitted in one go at the end. While this is the safest
82 and most correct time to emit these diagnostics, it can make debugging
83 these warnings difficult.
84
85 As of Test::NoWarnings 1.04 you can provide an experimental ":early"
86 pragma when loading the module to force warnings to be thrown via diag
87 at the time that they actually occur.
88
89 use Test::NoWarnings ':early';
90
91 As this will cause the diag to be emitted against the previous test and
92 not the one in which the warning actually occurred it is recommended
93 that the pragma be turned on only for debugging and left off when not
94 needed.
95
97 had_no_warnings
98 This checks that there have been warnings emitted by your test scripts.
99 Usually you will not call this explicitly as it is called automatically
100 when your script finishes.
101
102 clear_warnings
103 This will clear the array of warnings that have been captured. If the
104 array is empty then a call to "had_no_warnings()" will produce a pass
105 result.
106
107 warnings
108 This will return the array of warnings captured so far. Each element of
109 this array is an object containing information about the warning. The
110 following methods are available on these object.
111
112 · $warn->getMessage
113
114 Get the message that would been printed by the warning.
115
116 · $warn->getCarp
117
118 Get a stack trace of what was going on when the warning happened,
119 this stack trace is just a string generated by the Carp module.
120
121 · $warn->getTrace
122
123 Get a stack trace object generated by the Devel::StackTrace module.
124 This will return undef if Devel::StackTrace is not installed.
125
126 · $warn->getTest
127
128 Get the number of the test that executed before the warning was
129 emitted.
130
131 · $warn->getTestName
132
133 Get the name of the test that executed before the warning was
134 emitted.
135
137 When counting your tests for the plan, don't forget to include the test
138 that runs automatically when your script ends.
139
141 Bugs should be reported via the CPAN bug tracker at
142
143 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-NoWarnings>
144
145 For other issues, contact the author.
146
148 This was previously known as Test::Warn::None
149
151 Test::Builder, Test::Warn
152
154 Fergal Daly <fergal@esatclear.ie>
155
156 Adam Kennedy <adamk@cpan.org>
157
159 Copyright 2003 - 2007 Fergal Daly.
160
161 Some parts copyright 2010 - 2011 Adam Kennedy.
162
163 This program is free software and comes with no warranty. It is
164 distributed under the LGPL license
165
166 See the file LGPL included in this distribution or
167 http://www.fsf.org/licenses/licenses.html.
168
169
170
171perl v5.30.1 2020-01-30 Test::NoWarnings(3)