Tests! #57

Open
pawelmhm opened this Issue Jul 26, 2013 · 7 comments

Projects

None yet

2 participants

@pawelmhm

Hello,

thanks for maintaining this project, it's really cool useful.

The only problem I've found concerns running the tests in the template. In the tests.py there is a note that the code needs to be updated to Python 2.7 runtime, and I see that there was an issue here on github some time ago concerning this. I think it may be useful to open the issue once again.

I've fiddled around with this for a while, I've went through appengine docs on testing and simply took the code of their testrunner and added it to my app root directory (structure of which is based on your template), I had my own tests and edited them too to fit with gae.

The tests run ok and I'm partly happy with that but the only thing that bothers me is that my tests do not load testing configuration for flask's app.test_client. The tests take their configuration from main application directory and they ignore testing configuration that I pass to them in my tests setUp() method. This is not the end of the world since I'm still on localhost now, so I can find some ways to bypass this, but it slowly starts to irritate me and I'd like to improve this somehow.

So if anyone knows something that might be helpful here I'd be grateful about any information.

Once again thanks for maintaing the project!
Cheers!

@houmie
houmie commented Aug 9, 2013

Thanks for reporting this. I actually tried to run the tests and it wasn't very clear how to achieve this. Surely it can't be just python tests.py, right?

@pawelmhm
pawelmhm commented Aug 9, 2013

Well, I just read google documentation on it, created a test runner in src folder, it loads the tests from tests folder. Then you need to deal with the problem of loading configuration, I basically added classes to settings, three classes, dev, test, production, they all inherit some things from default configuration file.

Then there is the third probably most important thing, you need to tell Flask app to load configuration from a given object at runtime. I solved this issue by simply adding an environment variable FLASK_CONF, and set up things in application/init.py so that if the os.getenv('FLASK_CONF') returns "TEST" then flask loads settings for testing, if os.getenv returns "DEV" it loads config from other object, and so on.

It works ok in my code (but I changed a lot of other things compared with the basic version of this template). During the weekend I can check if it works okay with the Kamal's template, if it does I can submit a pull request to Kamal.

@houmie
houmie commented Oct 13, 2013

Hi, I was just wondering how your test suite are coming along and if you had a chance to submit a pull request to Kamal. :)

Many Thanks

@houmie
houmie commented Oct 14, 2013

I did many hours of research into this matter. It seems Google has now a new way to stub the services via testbed. Documentation is quite good: https://developers.google.com/appengine/docs/python/tools/localunittesting?csw=1

However the environment problems remain.

I can't import any of my models into the test. Because the import within models.py will import the application -> from application import app

which in turn fails with this error:

tests (unittest2.loader.ModuleImportFailure) ... ERROR

======================================================================
ERROR: tests (unittest2.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests
Traceback (most recent call last):
  File "/Users/hooman/workspace/F11A/src/lib/unittest2/loader.py", line 260, in _find_tests
    module = self._get_module_from_name(name)
  File "/Users/hooman/workspace/F11A/src/lib/unittest2/loader.py", line 238, in _get_module_from_name
    __import__(name)
  File "/Users/hooman/workspace/F11A/src/tests/tests.py", line 5, in <module>
    from application.rest_api_view import authenticate
  File "/Users/hooman/workspace/F11A/src/application/__init__.py", line 7, in <module>
    from gae_mini_profiler import profiler, templatetags
  File "/Users/hooman/workspace/F11A/src/lib/gae_mini_profiler/profiler.py", line 34, in <module>
    dev_server = os.environ["SERVER_SOFTWARE"].startswith("Devel")
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'SERVER_SOFTWARE'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

This project comes already with ExampleModel.py which is done very well.
It would be really beneficial to have a working set of unit tests for ExampleModel.py

This would help so many people understand how unittesting is done in GAE and actually unit test their apps based on this project.

@pawelmhm

Hi,

I submitted pull request.

As for the the error you're getting here it can be solved by simply setting environment variable SERVER_SOFTWARE to "Devel" - this is required by gae_mini_profiler, in ubuntu or any other linux simply do
export SERVER_SOFTWARE=Devel

or set it permanently. I added a comment about this in my pull request.

@houmie
houmie commented Oct 14, 2013

Thank you so much for your contribution. It finally works!!!

I am using Eclipse, hence I aded the SERVER_SOFTWARE=Devel to project's Python Interpreter's environment. And for each run configuration I have added under its environment the FLASK_CONF=TEST or FLASK_CONF=DEV accordingly. And it works like a charm.

The division of settings into Dev, test and production was also a great idea. So far I had a less fine-grained approach like this:

 # Auto-set debug mode based on App Engine dev environ
 if 'SERVER_SOFTWARE' in os.environ and os.environ['SERVER_SOFTWARE'].startswith('Dev'):
     DEBUG_MODE = True

But your approach is better. A few more lines comment for all the environment settings above is still needed, but its very fine in total.

@houmie
houmie commented Jan 6, 2014

Guys, have you noticed with the latest Eclipse plugin, Pydev and GAE there is a problem when setting FLASK_CONF to DEV in Eclipse environment?

The line if os.getenv('FLASK_CONF') == 'DEV': is never true any more.

Any eclipse users here with the same problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment