1Test::Workflow(3) User Contributed Perl Documentation Test::Workflow(3)
2
3
4
6 Test::Workflow - Provide test grouping, reusability, and structuring
7 such as RSPEC and cases.
8
10 Test::Workflow provides tools for grouping and structuring tests. There
11 is also a facility to write re-usable tests. Test::Workflow test files
12 define classes. Tests within the files are defined as a type of
13 method.
14
15 Test::Workflow provides an RSPEC implementation. This implementation
16 includes "describe" blocks, "it" blocks, as well as "before_each",
17 "before_all", "after_each", "after_all". There are even two new types:
18 "around_each" and "around_all".
19
20 Test::Workflow provides a cases workflow. This workflow allows you to
21 create multiple cases, and multiple tests. Each test will be run under
22 each case. This allows you to write a test that should pass under
23 multiple conditions, then write a case that builds that specific
24 condition.
25
26 Test::Workflow provides a way to 'inherit' tests. You can write classes
27 that use Test::Workflow, and define test groups, but not run them.
28 These classes can then be imported into as many test classes as you
29 want. This is achieved with the "with_tests" function.
30
31 Test::Workflow gives you control over the order in which test groups
32 will be run. You can use the predefined 'random', 'ordered', or 'sort'
33 settings. You may also provide your own ordering function. This is
34 achieved using the "test_sort" function.
35
37 package MyTest;
38 use strict;
39 use warnings;
40
41 use Test::More;
42 use Test::Workflow;
43
44 with_tests qw/ Test::TemplateA Test::TemplateB /;
45 test_sort 'rand';
46
47 # Tests can be at the package level
48 use_ok( 'MyClass' );
49
50 tests loner => sub {
51 my $self = shift;
52 ok( 1, "1 is the loneliest number... " );
53 };
54
55 tests not_ready => (
56 todo => "Feature not implemented",
57 code => sub { ... },
58 );
59
60 tests very_not_ready => (
61 skip => "These tests will die if run"
62 code => sub { ... },
63 );
64
65 run_tests();
66 done_testing();
67
68 RSPEC WORKFLOW
69 Here setup/teardown methods are declared in the order in which they are
70 run, but they can really be declared anywhere within the describe block
71 and the behavior will be identical.
72
73 describe example => sub {
74 my $self = shift;
75 my $number = 0;
76 my $letter = 'A';
77
78 before_all setup => sub { $number = 1 };
79
80 before_each letter_up => sub { $letter++ };
81
82 # it() is an alias for tests()
83 it check => sub {
84 my $self = shift;
85 is( $letter, 'B', "Letter was incremented" );
86 is( $number, 2, "number was incremented" );
87 };
88
89 after_each reset => sub { $number = 1 };
90
91 after_all teardown => sub {
92 is( $number, 1, "number is back to 1" );
93 };
94
95 describe nested => sub {
96 # This nested describe block will inherit before_each and
97 # after_each from the parent block.
98 ...
99 };
100
101 describe maybe_later => (
102 todo => "We might get to this",
103 code => { ... },
104 );
105 };
106
107 describe addon => sub {
108 my $self = shift;
109
110 around_each localize_env => sub {
111 my $self = shift;
112 my ( $inner ) = @_;
113
114 local %ENV = ( %ENV, foo => 'bar' );
115
116 $inner->();
117 };
118
119 tests foo => sub {
120 is( $ENV{foo}, 'bar', "in the localized environment" );
121 };
122 };
123
124 CASE WORKFLOW
125 Cases are used when you have a test that you wish to run under several
126 tests conditions. The following is a trivial example. Each test will be
127 run once under each case. Beware! this will run (cases x tests), with
128 many tests and cases this can be a huge set of actual tests. In this
129 example 8 in total will be run.
130
131 Note: The 'cases' keyword is an alias to describe. case blocks can go
132 into any workflow and will work as expected.
133
134 cases check_several_numbers => sub {
135 my $number;
136 case two => sub { $number = 2 };
137 case four => sub { $number = 4 };
138 case six => sub { $number = 6 };
139 case eight => sub { $number = 8 };
140
141 tests is_even => sub {
142 ok( !$number % 2, "number is even" );
143 };
144
145 tests only_digits => sub {
146 like( $number, qr/^\d+$/i, "number is all digits" );
147 };
148 };
149
151 with_tests( @CLASSES )
152 Use tests defined in the specified classes.
153
154 test_sort( sub { my @tests = @_; ...; return @tests })
155 test_sort( 'sort' )
156 test_sort( 'random' )
157 test_sort( 'ordered' )
158 Declare how tests should be sorted.
159
160 cases( name => sub { ... })
161 cases( 'name', %params, code => sub { ... })
162 describe( name => sub { ... })
163 describe( 'name', %params, code => sub { ... })
164 Define a block that encapsulates workflow elements.
165
166 tests( name => sub { ... })
167 tests( 'name', %params, code => sub { ... })
168 it( name => sub { ... })
169 it( 'name', %params, code => sub { ... })
170 Define a test block.
171
172 case( name => sub { ... })
173 case( 'name', %params, code => sub { ... })
174 Define a case, each test will be run once per case that is defined
175 at the same level (within the same describe or cases block).
176
177 before_each( name => sub { ... })
178 after_each( name => sub { ... })
179 before_all( name => sub { ... })
180 after_all( name => sub { ... })
181 These define setup and teardown functions that will be run around
182 tests.
183
184 around_each( name => sub { ... })
185 around_all( name => sub { ... })
186 These are special additions to the setup and teardown methods. They
187 are used like so:
188
189 around_each localize_env => sub {
190 my $self = shift;
191 my ( $inner ) = @_;
192
193 local %ENV = ( %ENV, foo => 'bar' );
194
195 $inner->();
196 };
197
198 run_tests()
199 This will finalize the meta-data (forbid addition of new tests) and
200 run the tests.
201
203 Chad Granum exodist7@gmail.com
204
206 Copyright (C) 2013 Chad Granum
207
208 Test-Workflow is free software; Standard perl license.
209
210 Test-Workflow is distributed in the hope that it will be useful, but
211 WITHOUT ANY WARRANTY; without even the implied warranty of
212 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license
213 for more details.
214
215
216
217perl v5.32.0 2020-07-28 Test::Workflow(3)