Skip to content

Commit

Permalink
functional_test command cleanup, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
skoczen committed Nov 18, 2011
1 parent d7ddf59 commit 0d4cc51
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ Requires: (inital thoughts, don't trust these yet.)
settings.py

```
FUNCTIONAL_TEST_SERVER_SETTINGS = "env.dev"
FORCE_SELENIUM_TESTS = False
SELENIUM_BROWSER_COMMAND = "*chrome"
# Defaults:
LIVE_SERVER_PORT = 8099
SELENIUM_PORT = 64444
VIRTUALENV_PATH = "" # Assumes `functional_tests` is installed in a virtualenv.
```

How to use
Expand Down
36 changes: 36 additions & 0 deletions functional_tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys

def exception_string():
import traceback
return '\n'.join(traceback.format_exception(*sys.exc_info()))

def print_exception():
print "######################## Exception #############################"
print exception_string()
print "################################################################"

def silence_print():
old_printerators=[sys.stdout,sys.stderr,sys.stdin,sys.__stdout__,sys.__stderr__,sys.__stdin__][:]
sys.stdout,sys.stderr,sys.stdin,sys.__stdout__,sys.__stderr__,sys.__stdin__=dummyStream(),dummyStream(),dummyStream(),dummyStream(),dummyStream(),dummyStream()
return old_printerators

def unsilence_print(printerators):
sys.stdout,sys.stderr,sys.stdin,sys.__stdout__,sys.__stderr__,sys.__stdin__=printerators


class dummyStream:
''' dummyStream behaves like a stream but does nothing. '''
# via http://www.answermysearches.com/python-temporarily-disable-printing-to-console/232/
def __init__(self): pass
def write(self,data): pass
def read(self,data): pass
def flush(self): pass
def close(self): pass

def noprint(func):
def wrapper(*args, **kw):
_p = silence_print()
output = func(*args, **kw)
unsilence_print(_p)
return output
return wrapper

This file was deleted.

Binary file not shown.
44 changes: 25 additions & 19 deletions functional_tests/management/commands/selenium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,63 @@
import subprocess
from os.path import abspath, join
import time
from qi_toolkit.helpers import print_exception
from functional_tests.helpers import print_exception

class Command(BaseCommand):
help = "Run the selenium tests."
__test__ = False

def handle(self, *args, **options):
output = file('/dev/null', 'a+')
VERBOSE = False
if VERBOSE:
silent_output = file('/dev/null', 'a+')
verbosity = int(options.get('verbosity', 1))
base_port = int(options.get('base_port', 0))

if verbosity > 1:
outputs={}
else:
outputs = {
"stderr":output,
"stdout":output,
"stderr":silent_output,
"stdout":silent_output,
}

test_settings = getattr(settings,"FUNCTIONAL_TEST_SERVER_SETTINGS","")
if test_settings != "":
test_settings = "--settings=%s" % test_settings

lots_of_options_dict = {
've_path': settings.VIRTUALENV_PATH,
'http_port': settings.LIVE_SERVER_PORT,
'test_server_settings': settings.SELENIUM_TEST_SERVER_SETTINGS,
'lib_path' : join(abspath(settings.PROJECT_ROOT), "lib"),
"selenium_port": settings.SELENIUM_PORT,
've_path': getattr(settings,"VIRTUALENV_PATH", abspath(join(abspath(__file__),"..","..","..","..","..",".."))),
'http_port': getattr(settings,"LIVE_SERVER_PORT",8099)+base_port,
'test_server_settings': test_settings,
'lib_path' : abspath(join(abspath(__file__), "..", "selenium_lib")),
"selenium_port": getattr(settings,"SELENIUM_PORT",4444)+base_port,
'file_uploader_port': getattr(settings,"FUNCTIONAL_TEST_FILE_UPLOADER_PORT", 8199)+base_port
}

sel_command = "java -jar %(lib_path)s/selenium-server.jar -timeout 30 -port %(selenium_port)s -userExtensions %(lib_path)s/user-extensions.js" % lots_of_options_dict
gun_command = "%(ve_path)s/bin/python manage.py run_gunicorn -w 2 -b 0.0.0.0:%(http_port)s --settings=envs.%(test_server_settings)s" % lots_of_options_dict
cel_command = "%(ve_path)s/bin/python manage.py celeryd --settings=envs.%(test_server_settings)s" % lots_of_options_dict
file_uploader_command = "%(ve_path)s/bin/python -m SimpleHTTPServer 8199" % lots_of_options_dict
gun_command = "%(ve_path)s/bin/python manage.py run_gunicorn -w 2 -b 0.0.0.0:%(http_port)s %(test_server_settings)s" % lots_of_options_dict
cel_command = "%(ve_path)s/bin/python manage.py celeryd %(test_server_settings)s" % lots_of_options_dict
file_uploader_command = "%(ve_path)s/bin/python -m SimpleHTTPServer %(file_uploader_port)s" % lots_of_options_dict

selenium_subprocess = subprocess.Popen(sel_command,shell=True, **outputs )
file_uploader_subprocess = subprocess.Popen(file_uploader_command,shell=True, cwd=join(settings.PROJECT_ROOT,"templates/test_file_uploads"), **outputs )
file_uploader_subprocess = subprocess.Popen(file_uploader_command,shell=True, cwd=join(settings.PROJECT_ROOT,"templates/functional_test_uploads"), **outputs )
time.sleep(10)
gunicorn_subprocess = subprocess.Popen(gun_command,shell=True, **outputs )
celery_subprocess = subprocess.Popen(cel_command,shell=True, **outputs )

try:
call_command('test', "--with-selenium", *args, **options )
output.close()
silent_output.close()
except:
print_exception()
pass

try:
selenium_subprocess.kill()
gunicorn_subprocess.kill()
except:
pass

try:
gunicorn_subprocess.kill()
selenium_subprocess.kill()
except:
pass

Expand All @@ -70,4 +76,4 @@ def handle(self, *args, **options):


print "Stopping..."
time.sleep(6)
time.sleep(8)

0 comments on commit 0d4cc51

Please sign in to comment.