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
.
|
|
|
|
|
|
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.
|
|
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.
|
|
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
|
|
Now let’s take a look at it all together.
|
|
Pretty simple huh? Hope this means more dzil
modules getting tested now. Including more of mine.