1Test2::Tools::Basic(3)User Contributed Perl DocumentationTest2::Tools::Basic(3)
2
3
4
6 Test2::Tools::Basic - Test2 implementation of the basic testing tools.
7
9 This is a Test2 based implementation of the more basic tools originally
10 provided by Test::More. Not all Test::More tools are provided by this
11 package, only the basic/simple ones. Some tools have been modified for
12 better diagnostics capabilities.
13
15 use Test2::Tools::Basic;
16
17 ok($x, "simple test");
18
19 if ($passing) {
20 pass('a passing test');
21 }
22 else {
23 fail('a failing test');
24 }
25
26 diag "This is a diagnostics message on STDERR";
27 note "This is a diagnostics message on STDOUT";
28
29 {
30 my $todo = todo "Reason for todo";
31 ok(0, "this test is todo");
32 }
33
34 ok(1, "this test is not todo");
35
36 todo "reason" => sub {
37 ok(0, "this test is todo");
38 };
39
40 ok(1, "this test is not todo");
41
42 SKIP: {
43 skip "This will wipe your drive";
44
45 # This never gets run:
46 ok(!system('sudo rm -rf /'), "Wipe drive");
47 }
48
49 done_testing;
50
52 All subs are exported by default.
53
54 PLANNING
55 plan($num)
56 plan('tests' => $num)
57 plan('skip_all' => $reason)
58 Set the number of tests that are expected. This must be done first
59 or last, never in the middle of testing.
60
61 For legacy compatibility you can specify 'tests' as the first
62 argument before the number. You can also use this to skip all with
63 the 'skip_all' prefix, followed by a reason for skipping.
64
65 skip_all($reason)
66 Set the plan to 0 with a reason, then exit true. This should be
67 used before any tests are run.
68
69 done_testing
70 Used to mark the end of testing. This is a safe way to have a
71 dynamic or unknown number of tests.
72
73 bail_out($reason)
74 Invoked when something has gone horribly wrong: stop everything,
75 kill all threads and processes, end the process with a false exit
76 status.
77
78 ASSERTIONS
79 ok($bool)
80 ok($bool, $name)
81 ok($bool, $name, @diag)
82 Simple assertion. If $bool is true the test passes, and if it is
83 false the test fails. The test name is optional, and all arguments
84 after the name are added as diagnostics message if and only if the
85 test fails. If the test passes all the diagnostics arguments will
86 be ignored.
87
88 pass()
89 pass($name)
90 Fire off a passing test (a single Ok event). The name is optional
91
92 fail()
93 fail($name)
94 fail($name, @diag)
95 Fire off a failing test (a single Ok event). The name and
96 diagnostics are optional.
97
98 DIAGNOSTICS
99 diag(@messages)
100 Write diagnostics messages. All items in @messages will be joined
101 into a single string with no separator. When using TAP, diagnostics
102 are sent to STDERR.
103
104 note(@messages)
105 Write note-diagnostics messages. All items in @messages will be
106 joined into a single string with no separator. When using TAP,
107 notes are sent to STDOUT.
108
109 META
110 $todo = todo($reason)
111 todo $reason => sub { ... }
112 This is used to mark some results as TODO. TODO means that the test
113 may fail, but will not cause the overall test suite to fail.
114
115 There are two ways to use this. The first is to use a codeblock,
116 and the TODO will only apply to the codeblock.
117
118 ok(1, "before"); # Not TODO
119
120 todo 'this will fail' => sub {
121 # This is TODO, as is any other test in this block.
122 ok(0, "blah");
123 };
124
125 ok(1, "after"); # Not TODO
126
127 The other way is to use a scoped variable. TODO will end when the
128 variable is destroyed or set to undef.
129
130 ok(1, "before"); # Not TODO
131
132 {
133 my $todo = todo 'this will fail';
134
135 # This is TODO, as is any other test in this block.
136 ok(0, "blah");
137 };
138
139 ok(1, "after"); # Not TODO
140
141 This is the same thing, but without the "{...}" scope.
142
143 ok(1, "before"); # Not TODO
144
145 my $todo = todo 'this will fail';
146
147 ok(0, "blah"); # TODO
148
149 $todo = undef;
150
151 ok(1, "after"); # Not TODO
152
153 skip($why)
154 skip($why, $count)
155 This is used to skip some tests. This requires you to wrap your
156 tests in a block labeled "SKIP:". This is somewhat magical. If no
157 $count is specified then it will issue a single result. If you
158 specify $count it will issue that many results.
159
160 SKIP: {
161 skip "This will wipe your drive";
162
163 # This never gets run:
164 ok(!system('sudo rm -rf /'), "Wipe drive");
165 }
166
168 The source code repository for Test2-Suite can be found at
169 https://github.com/Test-More/Test2-Suite/.
170
172 Chad Granum <exodist@cpan.org>
173
175 Chad Granum <exodist@cpan.org>
176
178 Copyright 2018 Chad Granum <exodist@cpan.org>.
179
180 This program is free software; you can redistribute it and/or modify it
181 under the same terms as Perl itself.
182
183 See http://dev.perl.org/licenses/
184
185
186
187perl v5.34.0 2021-11-16 Test2::Tools::Basic(3)