[Django]-Writing Testcases for google cloud endpoints API written on top of django

1👍

I have managed to solve this problem by appending project’s lib folder to system path variable.

So part of your manage.py file should look like,

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
    sys.path.append(sys.path[0] + os.path.sep + 'lib')

Then run the test through django. Don’t do a nosetest.

python manage.py test

Note that your test files should have a name starts with test, so that django would consider it as a test file. One of my test file would look like,

import unittest
from django.test import TestCase
import endpoints
import webtest
from google.appengine.ext import testbed
from project.queries import CategoryQuery
from project.api.internal.categories import Categories


class Test(TestCase):

    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.setup_env(current_version_id='testbed.version')
        self.testbed.activate()
        self.testbed.init_all_stubs()

        self.category_a = CategoryQuery.create_category_by_name('Museums')

        app = endpoints.api_server([Categories], restricted=False)
        self.testapp = webtest.TestApp(app)

    def tearDown(self):
        self.testbed.deactivate()

    # Test the handler.
    def test_should_return_list_of_pois(self):
        msg = {}
        resp = self.testapp.post_json('/_ah/spi/Categories.get_list', msg, status='*')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json['categories'][0]['name'], 'Museums')

3👍

In appengine_config.py Change vendor.add('lib') to vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib')). The path your tests are being executed from aren’t relative to lib so you are seeing that error. Don’t forget to import os.

👤Josh J

Leave a comment