Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to make a module aware it is being tested?

by brian_d_foy (Abbot)
on Nov 02, 2007 at 23:13 UTC ( [id://648737]=note: print w/replies, xml ) Need Help??


in reply to How to make a module aware it is being tested?

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
  • Comment on Re: How to make a module aware it is being tested?

Replies are listed 'Best First'.
Re^2: How to make a module aware it is being tested?
by CountZero (Bishop) on Nov 03, 2007 at 09:44 UTC
    My use case is as follows:

    I get external files which I need to parse to extract the data I have to save into a database. I cannot change these external files as that would invalidate the testing of the whole parsing process.

    The next step is to save this data into the database, BUT

    • The database is live; and
    • the testing process might crash and fail to clean-up the data it inserted during testing.

    Hence my idea to change the primary keys of the data to be inserted, so

    • the other programs using the database recognize the data as test-data and will not use them; and
    • it is easy to manually delete the inserted test-data in case the test script crashed and failed to clean-up.

    For that to work I had thought it would be easiest if my module would "know" when it was being tested, so it could manipulate the test data as explained above. Rather than relying on environment variables, external semaphore files or additional attributes on the module's object (all of which a programmer might forget to set or provide), I was looking for a solution which could be built-in, but it seems there is no easy fool-proof automatic way to check if a module is being "tested".

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://648737]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found