开发者

How to differentiate model classes between webapp and unittest

I've started looking in to unittest when using google app engine. And it seems to bit tricky from what I've read. Since you can't (and not suppose to) run your test's against the datastore.

I've written an abstract class to emulate a datastore model class. And it quite works pretty nice returning mockup data on get, all, fetch and so on (only tried on a small scale) returning dbModel like results.

The one thing I haven't found a solution I'm satisfied with is how to differentiate which model class to use. I want to use the mock-ups for unit tests and the actual db.Model for when webapp is running.

My current solution looks like this in my .py containing all db.Models:

if 'SERVER_SOFTWARE' in os.environ:
    class dbTest(db.Model):
        content = db.StringProperty()
        comments = db.ListProperty(str)
else:
    class dbTest(Abstract):
        content = 'Test'
        comments = ['test1', 'test2']

And it kinda feels like it could break any minute. Is this the way to go or could one combine these as one class and if the db.Model is invoked properly use that else开发者_StackOverflow the mockup?


Check out gaetestbed (docs). It stubs out the datastore (and all the other services like memcache) and makes testing from the command line very easy. It ensures a clean environment before every test runs.

I personally think it is nicer than the other solutions I have seen.


Instead of messing your models.py I would go with gaeunit.

I've used it with success in a couple of projects and the features I like are:

  1. Just one file to add to your project (gaeunit.py) and you are almost done
  2. Gaeunit isolates the test datastore from the development store (i.e. tests don't pollute your development db)


Since you can't (and not suppose to) run your test's against the datastore.

This is not true. You can and should use the local datastore implementation as a test harness - there's no reason to waste your time creating mocks for every datastore behaviour. You can use a tool such as noseGAE or gaeunit, as suggested by other posters, but if you want to set it up yourself, see this snippet.


There's more than one problem you're trying to address here...

First, for running tests with GAE emulation you can take a look at gaeunit, which I like best. If you don't want to run them from the browser then you can take a look at noseGAE (part of nose). This should give you command-line testing.

Second, regarding your comment about about 'creating an overhead of dependencies' it sounds like you're searching for a good unit testing and mocking framework. These will let you mock out the database for tests which don't need to hit it. Try mox and mockito for python.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜