unittest on GAE has been a challenge for us as well. right now we trigger
our unittests via a controller run in the GAE dev server. it's not pretty,
but it mostly works and has the proper GAE env setup. I'd love to figure
out a cleaner way to make this happen.
here's the controller that we use to run the unittests:
def unittests():
"""
Admin page from which the unit tests can be run
"""
import os
from os.path import isfile, join
import sys
import glob
import logging
#get a list of the modules to test, and make it nice and readable
cdir = os.path.join('applications', request.application, 'tests')
if not os.path.isdir(cdir):
die("applications/%s/tests is not a directory"%request.application)
# Get the files in the /tests directory
onlyfiles = [ f for f in os.listdir(cdir) if isfile(join(cdir,f)) ]
# Strip __init__.py
onlyfiles.remove('__init__.py')
# Name them nicely, for the javascript
correctly_named_files = [ f[:-3] for f in onlyfiles if f[-3:] == '.py']
return dict(
filename_list=correctly_named_files
)
def run_one_test():
"""Controller that ajax POSTs too from the unittest page to
get the result of one unit test in html form.
Returns this result as a string
"""
import os
import sys
import cStringIO
from gluon.shell import env
from google.appengine.api import namespace_manager
#save stdout so we can capture data and reset it.
stdout = sys.stdout
stderr = sys.stderr
#get a list of the modules to test
test_file = os.path.join('applications', request.application, 'tests/',
'%s.py'%request.args[0])
html = ''
namespace_manager.set_namespace('myapp_test')
globs = env(request.application, c='default', f='index',
import_models=True, extra_request={'test_db':True})
sys.stdout = cStringIO.StringIO()
execfile(test_file, globs)
report = sys.stdout.getvalue().strip()
if report.find('FAIL') >= 0:
html += '<h3 class="failed">FAILED</h3>\n'
html += CODE(report, language='web2py', \
link='/examples/global/vars/').xml()
else:
html += '<h3 class="passed">PASSED</h3>\n'
sys.stdout = stdout
return html
So then we launch GAE dev server, and visit our tests/unittests
controller. from there we select a test to run and it is run in the GAE
environment. note that we set a different GAE namespace for running tests
in a clean DB. We found that executing in a running controller was easier
to setup the GAE environment properly then to get all the needed WSGI vars
in the shell.
cfh
On Sunday, October 13, 2013 5:35:14 AM UTC-7, Quint wrote:
>
> Hi,
>
> I am trying to create a test for one of my modules.
> This module depends on several global variables like "Resonse" and some
> extra things i store in current from one of my Models. (current.myapp.*** ).
> So to be able to run the test i need to execute some of the models like
> this. (Is this the correct way?)
> (The testbed stuff is for GAE to create stubs for GAE services)
>
> def setUp (self):
> self.testbed = testbed.Testbed()
> self.testbed.activate()
> self.testbed.init_datastore_v3_stub()
> self.testbed.init_memcache_stub()
> exec_environment('applications/init/models/0.py')
> exec_environment('applications/init/models/db.py')
>
> I am using GAE with web2py 2.6.4 source and Python 2.7.3
>
> Now when i run the test i get this error:
>
> No handlers could be found for logger "web2py"
> DEBUG: connect attempt 0, connection error:
> Traceback (most recent call last):
> File "C:\Users\Quint\Documents\Projects\GAE\*******\gluon\dal.py", l
> ine 7854, in __init__
> self._adapter = ADAPTERS[self._dbname](**kwargs)
> File "C:\Users\Quint\Documents\Projects\GAE\*******\gluon\dal.py", l
> ine 2288, in __init__
> if do_connect: self.find_driver(adapter_args)
> File "C:\Users\Quint\Documents\Projects\GAE\*******\gluon\dal.py", l
> ine 791, in find_driver
> raise RuntimeError("no driver available %s" % str(self.drivers))
> RuntimeError: no driver available ('sqlite2', 'sqlite3')
>
> So it looks like DAL is instantiated and by default it tries to connect to
> sqlite?
>
> What can i do about this?
>
> BTW when i try to start the web2py shell i get a similar error only now i
> says that it find a "google" driver:
>
> No handlers could be found for logger "web2py"
> web2py Web Framework
> Created by Massimo Di Pierro, Copyright 2007-2013
> Version 2.6.4-stable+timestamp.2013.09.22.01.43.37
> Database drivers available: google
> DEBUG: connect attempt 0, connection error:
> Traceback (most recent call last):
> File "X:\GAE\*******\gluon\dal.py", line 7854, in __init__
> self._adapter = ADAPTERS[self._dbname](**kwargs)
> File "X:\GAE\*******\gluon\dal.py", line 2288, in __init__
> if do_connect: self.find_driver(adapter_args)
> File "X:\GAE\*******\gluon\dal.py", line 791, in find_driver
> raise RuntimeError("no driver available %s" % str(self.drivers))
> RuntimeError: no driver available ('sqlite2', 'sqlite3')
>
> Thanks!
>
>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.