Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

  • What is the best way to let an installing person define the dsn and credentials for a database to connect to for testing? - I am not sure about 'best', but I have used an environment variable, and have a skip_all call if the variable is not set or cannot be used for some other reason. The test uses the credentials when setting up the thing under test when DB credentials are absolutely necessary. If, however, the DB stuff can be mocked out, I do that.
  • What is the best way to encapsulate the functionality of reading this information and connecting to that database for providing a dbh to test routines? I want to use this functionality in several test files. Unless this is a test that is specifically addressing the database side of things, I would recommend mocking as much of the DB stuff away as possible. I have been doing a bit of this lately as I touch code written when I was "less experienced" (What frobnitz wrote this?). It seems to me that any time you pull data from the database, you should instead abstract that out to a function call that returns data, and possibly out to its own module. The function can then be mocked in testing, and you only need to test the DB function for what it is explicitly supposed to do. This is a generalization, and should be taken with the right amount of skepticism :-).
  • Is there a way to connect only once to a database for all tests in the t-directory of a distribution? A kind of test suite global. No(*). Each .t script is run in its own process.

* - I suppose something could be attempted on some operating systems of opening the DB handle in a wrapper process, setting it up to be passed through some IPC method, and launching your test script once that setup is complete. It seems to me that this would be very fragile and too clever to maintain.


In reply to Re: Setting up database connection in tests by MidLifeXis
in thread Setting up database connection in tests by McA

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2018-06-23 22:31 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.