~ubuntu-branches/ubuntu/trusty/libtest-roo-perl/trusty-proposed

« back to all changes in this revision

Viewing changes to README

  • Committer: Package Import Robot
  • Author(s): gregor herrmann
  • Date: 2014-01-14 19:25:11 UTC
  • Revision ID: package-import@ubuntu.com-20140114192511-9ycl4dc1zgamfnek
Tags: upstream-1.002
ImportĀ upstreamĀ versionĀ 1.002

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
NAME
 
2
    Test::Roo - Composable, reusable tests with roles and Moo
 
3
 
 
4
VERSION
 
5
    version 1.002
 
6
 
 
7
SYNOPSIS
 
8
    Define test behaviors and required fixtures in a role:
 
9
 
 
10
        # t/lib/ObjectCreation.pm
 
11
 
 
12
        package ObjectCreation;
 
13
        use Test::Roo::Role;    # loads Moo::Role and Test::More
 
14
 
 
15
        requires 'class';       # we need this fixture
 
16
 
 
17
        test 'object creation' => sub {
 
18
            my $self = shift;
 
19
            require_ok( $self->class );
 
20
            my $obj  = new_ok( $self->class );
 
21
        };
 
22
 
 
23
        1;
 
24
 
 
25
    Provide fixtures and run tests from the .t file:
 
26
 
 
27
        # t/test.t
 
28
 
 
29
        use Test::Roo; # loads Moo and Test::More
 
30
        use lib 't/lib';
 
31
 
 
32
        # provide the fixture
 
33
        has class => (
 
34
            is      => 'ro',
 
35
            default => sub { "Digest::MD5" },
 
36
        );
 
37
 
 
38
        # specify behaviors to test 
 
39
        with 'ObjectCreation';
 
40
 
 
41
        # give our subtests a pretty label
 
42
        sub _build_description { "Testing " . shift->class }
 
43
 
 
44
        # run the test with default fixture
 
45
        run_me;
 
46
 
 
47
        # run the test with different fixture
 
48
        run_me( { class => "Digest::SHA1" } );
 
49
 
 
50
        done_testing;
 
51
 
 
52
    Result:
 
53
 
 
54
        $ prove -lv t
 
55
        t/test.t .. 
 
56
                ok 1 - require Digest::MD5;
 
57
                ok 2 - The object isa Digest::MD5
 
58
                1..2
 
59
            ok 1 - object creation
 
60
            1..1
 
61
        ok 1 - Testing Digest::MD5
 
62
                ok 1 - require Digest::SHA1;
 
63
                ok 2 - The object isa Digest::SHA1
 
64
                1..2
 
65
            ok 1 - object creation
 
66
            1..1
 
67
        ok 2 - Testing Digest::SHA1
 
68
        1..2
 
69
        ok
 
70
        All tests successful.
 
71
        Files=1, Tests=2,  0 wallclock secs ( 0.02 usr  0.01 sys +  0.06 cusr  0.00 csys =  0.09 CPU)
 
72
        Result: PASS
 
73
 
 
74
DESCRIPTION
 
75
    This module allows you to compose Test::More tests from roles. It is
 
76
    inspired by the excellent Test::Routine module, but uses Moo instead of
 
77
    Moose. This gives most of the benefits without the need for Moose as a
 
78
    test dependency.
 
79
 
 
80
    Test files are Moo classes. You can define any needed test fixtures as
 
81
    Moo attributes. You define tests as method modifiers -- similar in
 
82
    concept to "subtest" in Test::More, but your test method will be passed
 
83
    the test object for access to fixture attributes. You may compose any
 
84
    Moo::Role into your test to define attributes, require particular
 
85
    methods, or define tests.
 
86
 
 
87
    This means that you can isolate test *behaviors* into roles which
 
88
    require certain test *fixtures* in order to run. Your main test file
 
89
    will provide the fixtures and compose the roles to run. This makes it
 
90
    easy to reuse test behaviors.
 
91
 
 
92
    For example, if you are creating tests for Awesome::Module, you could
 
93
    create the test behaviors as Awesome::Module::Test::Role and distribute
 
94
    it with your module. If another distribution subclasses Awesome::Module,
 
95
    it can compose the Awesome::Module::Test::Role behavior for its own
 
96
    tests.
 
97
 
 
98
    No more copying and pasting tests from a super class! Superclasses
 
99
    define and share their tests. Subclasses provide their own fixtures and
 
100
    run the tests.
 
101
 
 
102
USAGE
 
103
    Importing Test::Roo also loads Moo (which gives you strictures with
 
104
    fatal warnings and other goodies) and makes the current package a
 
105
    subclass of Test::Roo::Class.
 
106
 
 
107
    Importing also loads Test::More. No test plan is used. The
 
108
    "done_testing" function must be used at the end of every test file. Any
 
109
    import arguments are passed through to Test::More's "import" method.
 
110
 
 
111
    See also Test::Roo::Role for test role usage.
 
112
 
 
113
  Creating fixtures
 
114
    You can create fixtures with normal Moo syntax. You can even make them
 
115
    lazy if you want:
 
116
 
 
117
        has fixture => (
 
118
            is => 'lazy'
 
119
        );
 
120
 
 
121
        sub _build_fixture { ... }
 
122
 
 
123
    This becomes really useful with Test::Roo::Role. A role could define the
 
124
    attribute and require the builder method to be provided by the main test
 
125
    class.
 
126
 
 
127
  Composing test roles
 
128
    You can use roles to define units of test behavior and then compose them
 
129
    into your test class using the "with" function. Test roles may define
 
130
    attributes, declare tests, require certain methods and anything else you
 
131
    can regularly do with roles.
 
132
 
 
133
        use Test::Roo;
 
134
 
 
135
        with 'MyTestRole1', 'MyTestRole2';
 
136
 
 
137
    See Test::Roo::Role and the Test::Roo::Cookbook for details and
 
138
    examples.
 
139
 
 
140
  Setup and teardown
 
141
    You can add method modifiers around the "setup" and "teardown" methods
 
142
    and these will be run before tests begin and after tests finish
 
143
    (respectively).
 
144
 
 
145
        before  setup     => sub { ... };
 
146
 
 
147
        after   teardown  => sub { ... };
 
148
 
 
149
    You can also add method modifiers around "each_test", which will be run
 
150
    before and after every individual test. You could use these to prepare
 
151
    or reset a fixture.
 
152
 
 
153
        has fixture => ( is => 'lazy, clearer => 1, predicate => 1 );
 
154
 
 
155
        after  each_test => sub { shift->clear_fixture };
 
156
 
 
157
    Roles may also modify "setup", "teardown", and "each_test", so the order
 
158
    that modifiers will be called will depend on when roles are composed. Be
 
159
    careful with "each_test", though, because the global effect may make
 
160
    composition more fragile.
 
161
 
 
162
    You can call test functions in modifiers. For example, you could confirm
 
163
    that something has been set up or cleaned up.
 
164
 
 
165
        before each_test => sub { ok( ! shift->has_fixture ) };
 
166
 
 
167
  Running tests
 
168
    The simplest way to use Test::Roo with a single .t file is to let the
 
169
    "main" package be the test class and call "run_me" in it:
 
170
 
 
171
        # t/test.t
 
172
        use Test::Roo; # loads Moo and Test::More
 
173
 
 
174
        has class => (
 
175
            is      => 'ro',
 
176
            default => sub { "Digest::MD5" },
 
177
        );
 
178
 
 
179
        test 'load class' => sub {
 
180
            my $self = shift;
 
181
            require_ok( $self->class );
 
182
        }
 
183
 
 
184
        run_me;
 
185
        done_testing;
 
186
 
 
187
    Calling "run_me(@args)" is equivalent to calling
 
188
    "__PACKAGE__->run_tests(@args)" and runs tests for the current package.
 
189
 
 
190
    You may specify an optional description or hash reference of constructor
 
191
    arguments to customize the test object:
 
192
 
 
193
        run_me( "load MD5" );
 
194
        run_me( { class => "Digest::MD5" } );
 
195
        run_me( "load MD5", { class => "Digest::MD5" } );
 
196
 
 
197
    See Test::Roo::Class for more about the "run_tests" method.
 
198
 
 
199
    Alternatively, you can create a separate package (in the test file or in
 
200
    a separate .pm file) and run tests explicitly on that class.
 
201
 
 
202
        # t/test.t
 
203
        package MyTest;
 
204
        use Test::Roo;
 
205
 
 
206
        use lib 't/lib';
 
207
 
 
208
        has class => (
 
209
            is       => 'ro',
 
210
            required => 1,
 
211
        );
 
212
 
 
213
        with 'MyTestRole';
 
214
 
 
215
        package main;
 
216
        use strictures;
 
217
        use Test::More;
 
218
 
 
219
        for my $c ( qw/Digest::MD5 Digest::SHA/ ) {
 
220
            MyTest->run_tests("Testing $c", { class => $c } );
 
221
        }
 
222
 
 
223
        done_testing;
 
224
 
 
225
EXPORTED FUNCTIONS
 
226
    Loading Test::Roo exports subroutines into the calling package to
 
227
    declare and run tests.
 
228
 
 
229
  test
 
230
        test $label => sub { ... };
 
231
 
 
232
    The "test" function adds a subtest. The code reference will be called
 
233
    with the test object as its only argument.
 
234
 
 
235
    Tests are run in the order declared, so the order of tests from roles
 
236
    will depend on when they are composed relative to other test
 
237
    declarations.
 
238
 
 
239
  run_me
 
240
        run_me;
 
241
        run_me( $description );
 
242
        run_me( $init_args   );
 
243
        run_me( $description, $init_args );
 
244
 
 
245
    The "run_me" function calls the "run_tests" method on the current
 
246
    package and passes all arguments to that method. It takes a description
 
247
    and/or a hash reference of constructor arguments.
 
248
 
 
249
DIFFERENCES FROM TEST::ROUTINE
 
250
    While this module was inspired by Test::Routine, it is not a drop-in
 
251
    replacement. Here is an overview of major differences:
 
252
 
 
253
    *   Test::Roo uses Moo; Test::Routine uses Moose
 
254
 
 
255
    *   Loading Test::Roo makes the importing package a class; in
 
256
        Test::Routine it becomes a role
 
257
 
 
258
    *   Loading Test::Roo loads Test::More; Test::Routine does not
 
259
 
 
260
    *   In Test::Roo, "run_test" is a method; in Test::Routine it is a
 
261
        function and takes arguments in a different order
 
262
 
 
263
    *   In Test::Roo, all role composition must be explicit using "with"; in
 
264
        Test::Routine, the "run_tests" command can also compose roles
 
265
 
 
266
    *   In Test::Roo, test blocks become method modifiers hooked on an empty
 
267
        method; in Test::Routine, they become methods run via introspection
 
268
 
 
269
    *   In Test::Roo, setup and teardown are done by modifying "setup" and
 
270
        "teardown" methods; in Test::Routine they are done by modifying
 
271
        "run_test"
 
272
 
 
273
SUPPORT
 
274
  Bugs / Feature Requests
 
275
    Please report any bugs or feature requests through the issue tracker at
 
276
    <https://github.com/dagolden/Test-Roo/issues>. You will be notified
 
277
    automatically of any progress on your issue.
 
278
 
 
279
  Source Code
 
280
    This is open source software. The code repository is available for
 
281
    public review and contribution under the terms of the license.
 
282
 
 
283
    <https://github.com/dagolden/Test-Roo>
 
284
 
 
285
      git clone https://github.com/dagolden/Test-Roo.git
 
286
 
 
287
AUTHOR
 
288
    David Golden <dagolden@cpan.org>
 
289
 
 
290
CONTRIBUTOR
 
291
    Diab Jerius <djerius@gmail.com>
 
292
 
 
293
COPYRIGHT AND LICENSE
 
294
    This software is Copyright (c) 2013 by David Golden.
 
295
 
 
296
    This is free software, licensed under:
 
297
 
 
298
      The Apache License, Version 2.0, January 2004
 
299