Testing

Ferris provides utilities to test your application using nose and webtest.

Installing the nose plugin

FerrisNose is a nose plugin that allows you to run isolated Google App Engine tests. Install it via pip:

pip install ferrisnose

Note

You may have to run this with sudo on Linux and OS X.

Note

You may need to specify the –pre flag to pip.

Running tests

To run tests just use nose:

nosetests --with-ferris app/tests

Warning

be sure to specify a path or nose will try to discover all tests. We only need the test cases in /app/tests.

Writing tests for models

Models and other parts of the application that don’t involve HTTP/WSGI (such as services) can be tested using AppEngineTest.

class ferris.tests.lib.AppEngineTest(methodName='runTest')

Basic class from which all app engine test cases can inherit from. Handles setting up the testbed and provides utilities to log in users and run deferred tasks.

Here is a trivial example:

from app.models.cat import Cat
from ferrisnose import AppEngineTest

class CatTest(AppEngineTest):
    def test_herding(self):
        Cat(name="Pickles").put()
        Cat(name="Mr. Sparkles").put()

        assert Cat.query().count() == 2

Writing tests for controllers

Controller, components, and templates usually have to be tested within a full WSGI environment. This is where AppEngineWebTest comes in handy:

class ferris.tests.lib.AppEngineWebTest(methodName='runTest')

Provides a complete app engine testbed as well as a webtest instance available at self.testapp. You can add routes using self.add_route or add a ferris controller using self.add_controller.

To add controllers to the testapp use add_controller().

AppEngineWebTest.add_controller(c)

Here’s an example of writing a test case for a single controller:

from app.controllers.cats import Cats
from ferrisnose import AppEngineWebTest

class CatsTest(AppEngineWebTest):
    def test_herding_method(self):
        self.add_controller(Cats)

        r = self.testapp.get('/cats')

        assert "Pickles" in r

Writing tests for the entire application

Sometimes you want the entire application to be up and running. In this instance you can use FerrisAppTest. Like AppEngineWebTest it exposes a webtest instance via self.testapp but it automatically includes all controllers and plugins defined in your app.

class ferris.tests.lib.FerrisAppTest(methodName='runTest')

Provides a complete App Engine test environment and also automatically routes all application and plugin handlers to testapp.

This allows you to make requests to your application just as you would if the application were running:

from ferrisnose import FerrisAppTest

class CatsTest(FerrisAppTest):
    def test_herding(self):
        Cat(name="Pickles").put()
        Cat(name="Mr. Sparkles").put()

        r = self.testapp.get('/cats')

        assert "Pickles" in r
        assert "Mr. Sparkles" in r