I'm looking for suggestions on how to handle this. Is it better to require modules that are only needed for testing, or to skip the tests with a message to the user?

Three options:

  1. Include Struct::Compare in the PREREQ_PM of your modules Makefile.PL.
  2. If it's a pure-perl module then package it in with your tests. Stick it in t/lib and add an appropriate use lib line to the test scripts that need it.
  3. Skip the tests. If you need to skip a few tests in a test script you can use the method tachyon showed. If you need to skip an entire script it might be easier to use skip_all something like this (untested code):
    BEGIN { use Test::More; eval 'use Struct::Compare'; Test::More->builder->skip_all("need Struct::Compare") if $@; }; # rest of test script here

As to which is better... harder call. My personal tendency would be to do (1) if the tests were about vital functionality, and (3) if it related to things like checking documentation.

For example in Test::Class I require Test::Differences and a few other modules just for testing in the Makefile.PL. However the test I use for documentation are skipped if the relevant modules (Pod::Coverage, Pod::Checker and IO::String) are not available.

Also - you might want to look at Test::Deep if you're testing complicated structures. Might make your job easier.


In reply to Re: Optional modules for tests? by adrianh
in thread Optional modules for tests? by sutch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.