Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to prevent tests from running against production?

Reply
Thread Tools

How to prevent tests from running against production?

 
 
Roy Smith
Guest
Posts: n/a
 
      03-03-2013
Our deploy/configuration system includes credentials for connecting to a
database. We have one production database, and a variety of clones of
that in our test and development environments.

We've got a large body of tests, written with a combination of unittest
and nose. Many of our tests do things which shouldn't be done against
the production database. I'd like to set things up so anytime any test
code gets run, a check is made to see which database you're connected to
and if you're connect to production, the test refuses to run.

It's easy to write a check like that in setup(), but that only gets
called if you remember to write a setup() method (or inherit from
something that does). I'm looking for something that runs completely
automatically. I don't want somebody to be able to write something
which causes damage that nose can discover and run when you're connected
to the wrong database.
 
Reply With Quote
 
 
 
 
Ross Ridge
Guest
Posts: n/a
 
      03-04-2013
Roy Smith <> wrote:
>Our deploy/configuration system includes credentials for connecting to a
>database. We have one production database, and a variety of clones of
>that in our test and development environments.


Having your tests use credentials that don't work in the production
environment would seem to be the obvious solution.

Ross Ridge

--
l/ // Ross Ridge -- The Great HTMU
[oo][oo]
-()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
db //
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-04-2013
On Sun, 03 Mar 2013 16:56:16 -0500, Roy Smith wrote:

> Our deploy/configuration system includes credentials for connecting to a
> database. We have one production database, and a variety of clones of
> that in our test and development environments.
>
> We've got a large body of tests, written with a combination of unittest
> and nose. Many of our tests do things which shouldn't be done against
> the production database. I'd like to set things up so anytime any test
> code gets run, a check is made to see which database you're connected to
> and if you're connect to production, the test refuses to run.


Test code is just code, so in general you can't guarantee to idiot-proof
your tests. Anyone can write a function, called by one of the tests,
which connects directly to the production database and does whatever they
want. But I assume you're not worried about malice.

One solution: create a single module, used by all tests, that handles
connection to the database. Simply don't have your connection module
connect to the database you don't want it to connect to. Since all tests
go through that module for connections, you can enforce whatever access
rules you like.

For added security, you should ensure that the production database does
not accept the same credentials as the test databases.

Another solution: instead of calling unittest.TestCase directly, write
your own subclass, then have all your tests use that:

class MyTestCase(unittest.TestCase):
def __init__(self, *args, **kwargs):
if DATABASE == 'Production':
import webbrowser
webbrowser.open("http://i.imgur.com/y7Hm9.jpg", new=1)
raise RuntimeError("don't use production database")
super(MyTestCase, self).__init__(*args, **kwargs)


For added paranoia (and/or debugging fun), consider monkey-patching
unittest and/or nose.



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Best way to make a number of tests against an object('s attributes)with absence of switch statement? Phillip B Oldham Python 2 06-16-2008 09:06 AM
M$ against Blu-ray, M$ for Blu-ray, M$ against Blu-ray, M$ forBlu-ray, ...... Blig Merk DVD Video 66 04-27-2008 04:46 AM
Junit tests, setting up tests without having to create a billion methods xyzzy12@hotmail.com Java 8 02-28-2006 08:59 PM
Tests without study guides or practice tests? =?Utf-8?B?Q2hyaXNS?= Microsoft Certification 8 12-20-2005 04:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57