1
votes

I have a question concerning the way in which Python imports modules and creates objects within packages, I'm using Flask on Google App Engine but I think the problem is more general.

My app has the following structure:

app/
   application/
       __init__.py
       flaskr.py
       models.py
       mainConfig.py
       etc...
   tests/
       tests.py
       configtest.py
   apptest.py
   run.py

I am using Google App Engine launcher and it works okay most of the time. When I want to run the app I simply use dev_appserver.py. Appserver requires the path to my app as an argument, when it gets it it imports stuff from google app engine folder and executes my app. I'm happy with that.

The problem I'm having concerns my tests, most importantly I have a problem with the way tests load their configuration at runtime. I have separate configuration for tests, I don't want them to use mainConfig.py, they should use configTests.py. Unfortunately at the moment each time I run my tests the tests are using default app configuration and not their own specific options - for example they insert data to app database, and not to testing database that they have hardcoded in config.

I run tests through apptest.py, the file is fairly simple and it looks like this:

#!/usr/bin/python
import optparse
import sys
import unittest2

USAGE = """%prog SDK_PATH TEST_PATH
Run unit tests for App Engine apps.

SDK_PATH    Path to the SDK installation
TEST_PATH   Path to package containing test modules"""


def main(sdk_path, test_path):
    sys.path.insert(0, sdk_path)
    import dev_appserver
    dev_appserver.fix_sys_path()
    suite = unittest2.loader.TestLoader().discover(test_path)
    unittest2.TextTestRunner(verbosity=2).run(suite)

if __name__ == '__main__':
    SDK_PATH = u"pathToGoogle\\Google\\google_appengine"   
    TEST_PATH = u"pathtoapp\\app\\tests\\" 
    main(SDK_PATH, TEST_PATH)

I am trying to fiddle around this issue for a while, moving tests here or there within my application directory, editing code in my tests setUp method, but my fiddling around is quite pointless since I don't really understand what is going on here.

I know that app object is created in init within application folder, and it gets its settings from there. I should probably mention that nothing from the configuration in hardcoded in files in application folder, all files load the name of database by simply using app.config["DATABASE"](config is a dictionary like object, available everywhere within application). The main line in tests.py which sets up app.test_client goes like this:

from application import app

class FlaskrTestCase(unittest.TestCase):
    def setUp(self):
        app.config.from_object('configtest')
        self.app = app.test_client()

What is going on here, and how can one fix it? What should I do in order to force tests.py to get their configuration from their folder instead of getting it from application folder?

1

1 Answers

0
votes

I believe to solve this you should add an __init__.py file to the tests folder to make it a module and change app.config.from_object('configtest') to app.config.from_object('tests.configtest').