What was theuse case that you had where this would be useful? This sound a bit like an XY Problem.

Were you setting variables to different configuration values or something like that? Are you trying to mock a function? There's probably a much more robust way to accomplish your goal that doesn't involve magic or fragile code such as checking for a particular module. If you need to do this, the trick is to be able to control it yourself and not depend on something you can't control.

A case I have is credit card processing. I have a program that can charge a credit card. In the `make test` step of development, I don't want to charge the card. However, in deploying it, I also want to do a human test without charging the card. One of these test situations is under the test harness framework, and one isn't.

I use an environment variable. If I set the environment variable specifically tailored to my script (TEST_PROGRAM_NAME, for instance), which I can do in a program file or on the command line, my program can recognize the test condition with something I know that only I am affecting, I don't have to figure out acrobatics. Also, In my test harness stuff, I can turn the environment variable on and off, either in separate test scripts or even the same test script.

However, I can also do that with a configuration directive. I turn off or on features I need in the normal pogram configuration. That way I can change how the program acts without the test scripts having to invoke a magic environment variable to change behavior.

Another case I have is using the right database. In a test situation I have to hit a test database and in production I have to hit another. That's usually a matter of having the right configuration hooks. Inside the test situation though, often I want to test things that don't need the database, but at the semi-integration testing level I want to invoke the application, and the application wants to talk to the database. There I'll use some sort of Mock DBI object that pretends to connect to the database (and probably mocks returning the right data for particular calls). This is the same sort of thing I wold do if I needed my application to talk to the network, but I want to test it offline.

I don't want to recommend anything in particular without knowing what you are trying to accomplish, though. As with all program design problem, the trick is to reduce special cases. :)

And, finally, if your module acts differently under test conditions, how are you going to test how it acts not under test conditions? :)

--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

In reply to Re: How to make a module aware it is being tested? by brian_d_foy
in thread How to make a module aware it is being tested? by CountZero

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.