python - Can I make test dummies for every kind of service for an integrated app? -


i have complex app uses celery, mongo, redis , pyramid. use nose testing. i'm not doing tdd (not test-first, @ least), trying hard decent amount of coverage. i'm stuck in parts integrated of above services. example, i'm using redis shared memory between celery tasks, i'd able switch memcache without trouble, i've abstracted out following functions:

import settings db = strictredis(host=settings.db_uri, db=settings.db_name)  def has_task(correlation_id):     """return true if task exists in db."""     return db.exists(str(correlation_id))  def pop_task(correlation_id):     """get task db correlation_id."""     correlation_id = str(correlation_id) # no unicode allowed     task_pickle = db.get(correlation_id)     task = loads(task_pickle)     if task:         db.delete(correlation_id)     return task  def add_task(correlation_id, task_id, model):     """add task db"""     return db.set(str(correlation_id), dumps((task_id, model))) 

i'm doing similar things abstract mongo, i'm using persistent storage.

i've seen test suites integrated web apps run dummy http servers, create dummy requests , dummy databases. i'm ok celery , pyramid, haven't been able find dummies mongo , redis, i'm able run tests above when services running. there way provide dummy services above don't have:

  1. to have external services installed , running, and
  2. to manually create , destroy entire databases (in-memory dummies can counted on cleanup after themselves)

i suggest use mock library such tasks. allows replace production objects (for example database connection) pseudo objects provided functionality needed testing.

example:

>>> mock import mock >>> db = mock() >>> db.exists.return_value = true >>> db.exists() true 

you can make assertions how code interact mock, example:

>>> db.delete(1) <mock name='mock.delete()' id='37588880'> >>> db.delete.assert_called_with(1) >>> db.delete.assert_called_with(2) traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "c:\python27\lib\site-packages\mock.py", line 863, in assert_called_with     raise assertionerror(msg) assertionerror: expected call: delete(2) actual call: delete(1) 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -