The problem: your code sometimes does things that you do not want it to do while tests are running. Mocking up objects or methods is all fine and dandy, but if in the middle of a chunk of procedural code, you find yourself sending email by printing to a filehandle, that can be difficult to trap.

One solution is to wrap this code in a conditional, checking to see if $ENV{HARNESS_ACTIVE} or something is true. However, if you run your test script directly through perl and not through prove, that environment variable won't be set. One way of handling this is to use a custom Test::More module. The following snippet shows how to write one. It's a drop-in replacement for Test::More. You can include any behaviors you want, thereby making it easier for code to know if it's being run in a test environment.

package My::Test::More; use Test::Builder::Module; @ISA = qw(Test::Builder::Module); use Test::More; @EXPORT = @Test::More::EXPORT; # add whatever you need here $ENV{WE_BE_TESTING} = 1; 1;

In reply to Customize your Test::More by Ovid

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.