Hopefully, someone will use the blog post to write an actual doc patch, seeing as how this is undocumented.

I finally wrote A test for DZP::OurPkgVersion with the help of CJM. So I figure it’s best to share the knowledge imparted upon me to all those who are writing plugins without tests.

Before we get started I’m going to advise that this test will only check the output that dzil built, if you need it to test anything more sophisticated, you’ll have to learn more.

First you’ll want to create a corpus repo like /corpus/MyDZTRepo with a basic minimal repo. This repo is simply a repo that you are using to test your plugin against, to make sure it works right. You put it in corpus so that if you have tests that you have to check in your corpus, those tests themselves aren’t run when the test suite is run. The dist.ini doesn’t need to contain anymore than the basic stuff needed to build. You .pm files need not have anymore data than what you’re going to need to make your dzil plugin do its job. In the case of DZP::OurPkgVersion I only needed to test that the output found the # VERSION string correctly in a couple of scenario’s. So that meant having # VERSION in the .pm’s and [OurPkgVersion] in the dist.ini.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
name    = DZT0
author  = Caleb Cushing <xenoterracide@gmail.com>
license =  Artistic_2_0
version = 0.1.0
copyright_holder = Caleb Cushing

[@Filter]
-bundle = @Basic
-remove = Readme

[OurPkgVersion]
1
2
3
4
5
6
use strict;
use warnings;
package DZT0;
# VERSION
# ABSTRACT: my abstract
1;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Dist::Zilla::Tester;

# specify where the dist lives
my $tzil = Dist::Zilla::Tester->from_config({ dist_root => 'corpus/DZT' });

# function to essentially run dzil build
$tzil->build;
...

First DZT (Dist::Zilla::Tester) doesn’t provide any tests of its own so you still need to use Test::More or some other testing framework. Next you need to initialize the tester object by telling it where the root of your corpus repo is. After that, unless you need to do other work, you can run $tzil->build so that the build is run.

So now lets slurp a file into memory so we can check to see if it was built right. You’ll want to look in 'build/*' as the basic root of the build directory. So 'build/t/test1.t' if you need to slurp a test.

1
2
3
...
my $lib_0 = $tzil->slurp_file('build/lib/DZT0.pm');
...

Now that we’ve pulled our build files into memory lets code up what the result should be. We can just do this with a simple heredoc, obviously you can do it another way.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
...
my $elib_0 = <<'END LIB0';
use strict;
use warnings;
package DZT0;
our $VERSION = 0.1.0;# VERSION
# ABSTRACT: my abstract
1;
END LIB0
...

Now that we’ve gotten that, all we have to do is compare the file that we expect dzil to output and the file that dzil actually built. This is just standard Test::More

1
2
3
...
is ( $lib_0, $elib_0, 'check DZT0.pm' );
done_testing;

Now let’s take a look at it all together.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Dist::Zilla::Tester;

my $tzil = Dist::Zilla::Tester->from_config({ dist_root => 'corpus/DZT' });

$tzil->build;

my $lib_0 = $tzil->slurp_file('build/lib/DZT0.pm');

# e short for expected files
my $elib_0 = <<'END LIB0';
use strict;
use warnings;
package DZT0;
our $VERSION = 0.1.0;# VERSION
# ABSTRACT: my abstract
1;
END LIB0

is ( $lib_0, $elib_0, 'check DZT0.pm' );

done_testing;

Pretty simple huh? Hope this means more dzil modules getting tested now. Including more of mine.