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

It seems like a dubious idea at best to deliberately make a module act differently for a test, but if you're really dedicated to it, why not add a method or two, perhaps a $self->test getter and setter, followed by some internal logic (or whatever madness you got goin' on under the hood)? Then you call that method in your test scripts, and everything is fine.

Then you could write a test to see if your test method is doing what you think it is.

  • 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 01, 2007 at 21:34 UTC
    I agree with you that in general changing the behaviour of a module when it is being tested is not a good idea, but in this case I wish the module to add a distinguishing mark to the primary key field of a database record when it is being tested so the records inserted by the test cannot be mistaken for real records. The module will have to work with an active / life database and the records to be inserted are parsed from real life example files which I cannot change.

    Your idea of flagging to the module it is being tested is good, but it means I have to rewrite all my tests to add that flag and in the future may not forget to use that flag.

    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

      The module will have to work with an active / life database and the records to be inserted are parsed from real life example files which I cannot change.
      Setting up a database for testing by copying a few records from the live database sounds like a better idea.
        Ultimately that will indeed be the best idea, but it means I have to redo all my tests to set-up a SQLite database for testing purposes. I have put it on my TODO list for version 2.

        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