[TIP] Mocking a cursor for use with Psycopg2

Ben Finney ben+python at benfinney.id.au
Thu Mar 13 17:46:36 PDT 2014


Chris Withers <chris at simplistix.co.uk> writes:

> On 13/03/2014 16:29, Alexander Bandukwala wrote:
> > I'm trying to mock access to my postgres database using the mock
> > framework.
>
> Why mock postgres rather than just using a real postgres db?

A database is a very heavy dependency. Dependencies should be very small
and simple for unit tests; this is where mock objects are useful.

If one is not interested in testing the database, then the fact that it
takes a large amount of set-up to control exactly how it will behave is
too much of a burden for most unit tests.

For other kinds of tests – integration tests, acceptance tests,
performance tests, etc. – using the real DBMS is advisable. But for a
unit test, usually the full DBMS is not helpful and is a hindrance to
testing the code unit.

-- 
 \           “The cost of education is trivial compared to the cost of |
  `\                                     ignorance.” —Thomas Jefferson |
_o__)                                                                  |
Ben Finney




More information about the testing-in-python mailing list