Commit e459a8d5 authored by eckhart's avatar eckhart

- test/run.py now runs unit tests concurrently

parent 829fe9b0
......@@ -542,6 +542,12 @@ def grammar_suite(directory, parser_factory, transformer_factory,
return ''
#######################################################################
#
# general unit testing support
#
#######################################################################
def run_tests_in_class(test, namespace):
"""
......@@ -572,11 +578,12 @@ def run_tests_in_class(test, namespace):
obj.teardown()
def run_test_function(test, namespace):
def run_test_function(func_name, namespace):
"""
Run the test-function `test` in the given namespace.
"""
exec(test + '()', namespace)
print("Running test-function: " + func_name)
exec(func_name + '()', namespace)
def runner(test_classes, namespace):
......@@ -590,8 +597,8 @@ def runner(test_classes, namespace):
in such classes or functions, the name of which starts with "test".
Args:
tests: Either a string or a list of strings that contains the
names of test or test classes. Each test and, in the case
test_classes: Either a string or a list of strings that contains
the names of test or test classes. Each test and, in the case
of a test class, all tests within the test class will be
run.
namespace: The namespace for running the test, usually
......@@ -633,3 +640,31 @@ def runner(test_classes, namespace):
run_test_function(test, namespace)
def run_file(fname):
if fname.lower().startswith('test_') and fname.endswith('.py'):
# print('\nRUNNING UNIT TESTS IN: ' + fname)
exec('import ' + fname[:-3])
runner('', eval(fname[:-3]).__dict__)
def run_path(path):
"""Runs all unit tests in `path`"""
if os.path.isdir(path):
sys.path.append(path)
files = os.listdir(path)
result_futures = []
with concurrent.futures.ProcessPoolExecutor(multiprocessing.cpu_count()) as pool:
for f in files:
result_futures.append(pool.submit(run_file, f))
# run_file(f) # for testing!
for r in result_futures:
try:
_ = r.result()
except AssertionError as failure:
print(failure)
else:
path, fname = os.path.split(path)
sys.path.append(path)
run_file(fname)
sys.path.pop()
......@@ -12,11 +12,9 @@ import sys
import time
import threading
sys.path.extend(['../', './'])
lock = threading.Lock()
def run_doctests(module):
with lock:
namespace = {}
......@@ -26,11 +24,13 @@ def run_doctests(module):
result = doctest.testmod(mod)
return result.failed
def run_unittests(command):
args = command.split(' ')
filename = args[1]
print('\nUNITTEST ' + filename)
subprocess.run(args)
# def run_unittests(command):
# args = command.split(' ')
# filename = args[1]
# print('\nUNITTEST ' + filename)
# subprocess.run(args)
if __name__ == "__main__":
scriptdir = os.path.dirname(os.path.realpath(__file__))
......@@ -60,10 +60,15 @@ if __name__ == "__main__":
# unit tests
for interpreter in interpreters:
os.system(interpreter + '--version')
for filename in os.listdir('test'):
if filename.startswith('test_'):
command = interpreter + os.path.join('test', filename)
results.append(pool.submit(run_unittests, command))
# for filename in os.listdir('test'):
# if filename.startswith('test_'):
# command = interpreter + os.path.join('test', filename)
# results.append(pool.submit(run_unittests, command))
os.system(' '.join([interpreter, '-c',
"""'import sys;"""
"""sys.path.extend(["DHParser"]);"""
"""import testing; testing.run_path("%s")'""" % scriptdir,
os.path.join(os.getcwd(), 'test')]))
concurrent.futures.wait(results)
......
......@@ -63,15 +63,15 @@ class TestCompilerGeneration:
word = /\w+/
WSPC = /\s+/
"""
tmpname = 'tmp_' + concurrent_ident()
tmp = os.path.join('test', tmpname) if os.path.isdir('test/') else tmpname
trivial_text = u"""Es war ein Koenig in Thule.\n"""
grammar_name = os.path.join(tmp, "TestCompilerGeneration.ebnf")
compiler_name = os.path.join(tmp, "TestCompilerGenerationCompiler.py")
text_name = os.path.join(tmp, "TestCompilerGeneration_text.txt")
result_name = os.path.join(tmp, "TestCompilerGeneration_text.xml")
def setup(self):
self.tmpname = 'tmp_' + concurrent_ident()
self.tmp = os.path.join('test', self.tmpname) if os.path.isdir('test/') else self.tmpname
self.trivial_text = u"""Es war ein Koenig in Thule.\n"""
self.grammar_name = os.path.join(self.tmp, "TestCompilerGeneration.ebnf")
self.compiler_name = os.path.join(self.tmp, "TestCompilerGenerationCompiler.py")
self.text_name = os.path.join(self.tmp, "TestCompilerGeneration_text.txt")
self.result_name = os.path.join(self.tmp, "TestCompilerGeneration_text.xml")
if not os.path.exists(self.tmp):
os.mkdir(self.tmp)
with open(self.grammar_name, "w") as f:
......
......@@ -731,5 +731,4 @@ class TestAllOfResume:
if __name__ == "__main__":
from DHParser.testing import runner
runner("", globals())
......@@ -292,7 +292,7 @@ class TestSerialization:
assert tree.as_xml() == "<A>\n <B>C</B>\n <D>E</D>\n</A>", xml
tree.attr['xml:space'] = 'preserve'
print(tree.attr)
# print(tree.attr)
xml = tree.as_xml()
assert xml == '<A xml:space="preserve"><B>C</B><D>E</D></A>', xml
......
......@@ -33,14 +33,13 @@ from DHParser.log import log_dir, logging, is_logging
class TestLoggingAndLoading:
tmpname = 'tmp_' + concurrent_ident()
filename = os.path.join("test", tmpname, "test.py") if os.path.isdir('test') \
else os.path.join(tmpname, "test.py")
dirname = os.path.dirname(filename)
code1 = "x = 46"
code2 = "def f():\n return 46"
def setup(self):
self.tmpname = 'tmp_' + concurrent_ident()
self.filename = os.path.join("test", self.tmpname, "test.py") if os.path.isdir('test') \
else os.path.join(self.tmpname, "test.py")
self.dirname = os.path.dirname(self.filename)
self.code1 = "x = 46"
self.code2 = "def f():\n return 46"
if not os.path.exists(self.dirname):
os.mkdir(self.dirname)
with open(self.filename, 'w') as f:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment