Created
April 6, 2012 18:09
-
-
Save jagoda/2321727 to your computer and use it in GitHub Desktop.
django.test + DOH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Unit testing support for DOH. | |
from django.conf import settings | |
from django.test import TestCase | |
import os | |
import subprocess | |
from time import sleep | |
from urlparse import parse_qs as parse | |
class DOHTest(TestCase): | |
TEST_LOADER = settings.SUPPORT_URL + 'doh_loader.js' | |
TEST_RUNNER = 'http://localhost:8000' + settings.SUPPORT_URL + \ | |
'dojo-release-1.7.2-src/util/doh/runner.html?test=' + TEST_LOADER | |
@classmethod | |
def generate_test_methods (clazz, app_directory): | |
clazz._app_directory = app_directory | |
clazz.start_server() | |
# FIXME: need to get browser(s) from config | |
clazz.launch_browser('chromium-browser') | |
clazz.read_pipe() | |
clazz.stop_server() | |
@classmethod | |
def launch_browser (clazz, browser): | |
return subprocess.Popen([ '/usr/bin/env', browser, clazz.TEST_RUNNER ]) | |
@classmethod | |
def read_pipe (clazz): | |
buffer = [] | |
while(1): | |
c = os.read(clazz._pipe, 1) | |
if c == '\n': | |
line = ''.join(buffer) | |
buffer = [] | |
if line == 'END': | |
break | |
else: | |
data = parse(line) | |
group = data['group'][0] | |
source = data['source'][0] | |
success = data['success'][0] | |
test = data['test'][0] | |
group = group.replace(' ', '-') | |
test = test.replace(' ', '-') | |
test_name = 'test_{0}_{1}'.format(group, test) | |
print "Creating test '{0}'.".format(test_name) | |
def pass_test (self): | |
pass | |
def fail_test (self): | |
self.fail("\n\n\tFailed DOH test: '{0}:{1}' " \ | |
"(see DOH output for details).\n\n\tERROR IN:\n\n\t{2}".format( | |
group, test, source)) | |
if success == 'true': | |
setattr(clazz, test_name, pass_test) | |
else: | |
setattr(clazz, test_name, fail_test) | |
else: | |
buffer.append(c) | |
@classmethod | |
def start_server (clazz): | |
if not hasattr(clazz, '_server'): | |
clazz._pipe, child_pipe = os.pipe() | |
clazz._server = subprocess.Popen([ '/usr/bin/env', | |
'PIPE=' + str(child_pipe), 'APP_DIRECTORY=' + clazz._app_directory, | |
'python', 'manage.py', 'testserver' ]) | |
sleep(2) | |
return clazz._server | |
@classmethod | |
def stop_server (clazz): | |
if hasattr(clazz, '_server'): | |
clazz._server.kill() | |
os.close(clazz._pipe) | |
del clazz._server | |
del clazz._pipe | |
def create_test_case (name, app_directory): | |
test_class = type(name, (DOHTest,), {}) | |
test_class.generate_test_methods(app_directory) | |
return test_class | |
# Backend support for DOH unit testing. | |
from django.http import HttpResponse | |
from glob import glob | |
import json | |
import os.path as path | |
PIPE = int(os.environ['PIPE']) if 'PIPE' in os.environ else -1 | |
if 'APP_DIRECTORY' in os.environ: | |
APP_DIRECTORY = os.environ['APP_DIRECTORY'] | |
else: | |
APP_DIRECTORY = os.getcwd() | |
def all_tests (): | |
test_directory = path.join(APP_DIRECTORY, 'test') | |
offset = len(path.dirname(__file__)) | |
files = [] | |
for root, directory_names, file_names in os.walk(test_directory): | |
root = root[ offset: ] | |
for name in file_names: | |
if name[ -3: ] == '.js': | |
files.append(path.join(root, name)) | |
return files | |
def packages (): | |
static_directory = path.join(APP_DIRECTORY, 'static') | |
offset = len(static_directory) + 1 | |
def package_listing (filename): | |
filename = filename[ offset: ] | |
return { 'name': filename, 'location': filename } | |
package_definitions = map( | |
package_listing, | |
glob(path.join(static_directory, '*')) | |
) | |
package_definitions.extend([ | |
{ 'name': 'dojo', 'location': 'dojo' }, | |
{ 'name': 'dijit', 'location': 'dijit' }, | |
{ 'name': 'dojox', 'location': 'dojox' } | |
]) | |
return { | |
'baseUrl': settings.STATIC_URL, | |
'packages': package_definitions | |
} | |
def test_config (request): | |
config = { | |
'modules': all_tests(), | |
'packages': packages() | |
} | |
return HttpResponse(json.dumps(config)) | |
def stop (request): | |
os.write(PIPE, 'END\n') | |
os.close(PIPE) | |
return HttpResponse() | |
def test_run (request): | |
os.write(PIPE, request.read()) | |
os.write(PIPE, '\n') | |
return HttpResponse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define( | |
[ | |
'doh/runner', | |
'dojo/_base/xhr', | |
'dojo/io-query' | |
], | |
function (doh, xhr, ioQuery) { | |
var onEnd = doh._onEnd, | |
testFinished = doh._testFinished; | |
doh._onEnd = function () { | |
onEnd.apply(doh, arguments); | |
xhr.get({ url: '/stop' }); | |
}; | |
doh._testFinished = function (group, fixture, success) { | |
var args = Array.prototype.slice.call(arguments), | |
source = fixture.runTest.toSource ? | |
fixture.runTest.toSource() : | |
String(fixture.runTest); | |
xhr.post({ | |
url: '/test_run', | |
postData: ioQuery.objectToQuery({ | |
group: group, | |
source: source, | |
success: success, | |
test: fixture.name | |
}) | |
}); | |
testFinished.apply(doh, args); | |
}; | |
xhr.get({ | |
url: '/test_config', | |
handleAs: 'json', | |
sync: true, | |
load: function (data) { | |
require(data.packages, data.modules); | |
} | |
}); | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import doh | |
import os.path as path | |
MyTest = doh.create_test_case('MyTest', path.dirname(__file__)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment