Your test script can use File::Temp to create a file for you that you write to, and that you then can read back in your test. Done correctly, the file will be removed on end of scope. I usually prefer creating a tempdir with a template such as testname-XXXXX. That way I can create any files I want within that directory, and it will get torn down on close of scope; I don't have to bother with temp files for each thing that needs a file, so long as I put everything ephemeral into the tempdir.

A staunch unit-test advocate would say that unit tests shouldn't touch the filesystem unless they're specifically testing low level filesystem operations. It may be possible for your tests to open filehandles to in-memory files instead of touching the filesystem. The documentation for open discusses how to do this, I think. If it doesn't, then perlopentut would be the other place to look. But in short:

my $in_string = "Hello world.\n"; my $out_string = ''; open my $infh, '<', \$in_string; open my $outfh, '>', \$out_string; while(<$infh>) { print $outfh "$.: $_"; } print $out_string; # prints 1: Hello world.\n

So here I gave Perl a variable containing "Hello world.\n" and told it to treat it as a file, allowing $infh to stream through the pseudo-file. I also gave Perl an empty string for output, and printed the input file to the output file, with a line number prepended. Finally, I printed the contents of the output string, and it contained the contents of the input string, but with a line number prepended.

This technique is great for tests, because it allows you to test in isolation of the filesystem. Later, you can independently test that when you create files (in a tempdir) they have the appropriate names, permissions, and owner.

My preference is to test as much as possible in isolation of the filesystem, but then in more functional tests, also verify things like the characteristics of created files, and the ability to read the files we need to be able to read. That way I can do really fast testing of things like what contents are dumped to or read from files, but can also verify that I'm working effectively within the environment.


Dave


In reply to Re^3: Can Test::MockObject mock a file? by davido
in thread Can Test::MockObject mock a file? by Lady_Aleena

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.