Last active
May 19, 2017 16:56
-
-
Save pmolodo/e43d35498fa1be46389a4a79a5a8b479 to your computer and use it in GitHub Desktop.
utility funcs for gathering information about which tests were run
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
#execfile('/Volumes/home/paulm/Desktop/compare_tests.py') | |
# Some utility funcs for printing out the list of tests run - useful for | |
# ensuring that the same set of tests are run if/when we fully transition to | |
# pytest | |
unittest_run_re = re.compile(r'^(?:(?P<normal_testname>test[^ \n]*) \((?P<normal_classname>test[^\n]*)\)|Doctest: (?P<doctest_name>[a-zA-Z0-9_\.]+)) ... ', re.MULTILINE) | |
def getTestNames_unittest_run(logPath): | |
'''Parse test names from the output log of a "run_tests" (unittest) invocation''' | |
names = [] | |
with open(logPath, 'r') as f: | |
text = f.read() | |
for match in unittest_run_re.finditer(text): | |
if match.group('doctest_name'): | |
name = 'doctest.DocTestCase.runTest - {}'.format(match.group('doctest_name')) | |
else: | |
name = '{}.{}'.format(match.group('normal_classname'), match.group('normal_testname')) | |
names.append(name) | |
return names | |
unittest_collect_re = re.compile(r'^(?:(?P<normal_testname>[a-zA-Z0-9_\.]+)|doctest\.DocTestCase\.runTest - \<DocTest (?P<doctest_name>[a-zA-Z0-9_\.]+) from [^\n]*\:(?:[0-9]+|None) \([0-9]+ examples?\)\>)$', re.MULTILINE) | |
def getTestNames_unittest_collect(logPath): | |
'''Parse test names from the output log of a "run_tests --collect-only" (unittest) invocation''' | |
names = [] | |
with open(logPath, 'r') as f: | |
text = f.read() | |
for match in unittest_collect_re.finditer(text): | |
if match.group('doctest_name'): | |
name = 'doctest.DocTestCase.runTest - {}'.format(match.group('doctest_name')) | |
else: | |
name = match.group('normal_testname') | |
if '.' not in name: | |
continue | |
names.append(name) | |
return names | |
pytest_collect_re = re.compile(r'''^(?P<spaces>(?: )*)\<(?P<type>[^ ]+) \'(?P<name>[^']+)\'\>$''', re.MULTILINE) | |
def getTestNames_pytest_collect(logPath): | |
'''Parse test names from the output log of a "py.text --collect-only" (pytest) invocation''' | |
names = [] | |
numNonMatching = 0 | |
numFunctions = 0 | |
with open(logPath, 'r') as f: | |
text = f.read() | |
stack = [] | |
# for match in pytest_collect_re.finditer(text): | |
for line in text.splitlines(): | |
match = pytest_collect_re.match(line) | |
if not match: | |
numNonMatching += 1 | |
continue | |
spaces = match.group('spaces') | |
level = len(spaces) / 2 | |
objType = match.group('type') | |
if objType not in ('Module', 'UnitTestCase', 'TestCaseFunction', 'Function'): | |
# just to catch something unexpected | |
raise ValueError(objType) | |
if objType == 'Function': | |
numFunctions += 1 | |
continue | |
name = match.group('name') | |
if not name: | |
raise RuntimeError(line) | |
if objType == 'Module': | |
if name.endswith('.py'): | |
name = name[:-3] | |
name = name.replace('/', '.') | |
stack[level:] = [name] | |
if objType == 'TestCaseFunction': | |
names.append(".".join(stack)) | |
print "numFunctions:", numFunctions | |
print "numNonMatching:", numNonMatching | |
return names | |
import sys | |
helpers_path = '/Volumes/sv-dev01/devRepo/paulm/pipe/maya/tests' | |
if helpers_path not in sys.path: | |
sys.path.append(helpers_path) | |
import luma_test_helpers | |
reload(luma_test_helpers) | |
from luma_test_helpers import getTestNames_unittest_run, getTestNames_unittest_collect, getTestNames_pytest_collect | |
# <TestCaseFunction 'test_01_directory'> | |
def getLogPath(logPath): | |
if '/' not in logPath: | |
logPath = os.path.join('/Volumes/sv-dev01/devRepo/paulm/pipe/python/tests', logPath) | |
return logPath | |
#names1 = getTestNames_unittest_run('/Volumes/sv-dev01/devRepo/paulm/pipe/python/tests/test_output_2017-04-24_11-32-12.unittest.log') | |
# test_cpremote - 31 | |
#names1 = getTestNames_unittest_run(getLogPath('test_output_2017-04-24_12-47-06.unittest.log')) | |
# test_mongo - 1 | |
names1 = getTestNames_unittest_run(getLogPath('test_output_2017-04-24_12-49-33.unittest.log')) | |
print len(names1) | |
set1 = set(names1) | |
print len(set1) == len(names1) | |
# all names - before doctest fixes | |
# names2 = getTestNames_unittest_collect('/Volumes/sv-dev01/devRepo/paulm/pipe/python/tests/test_output_2017-04-24_11-40-28.unittest.log') | |
# all names - after doctest fixes | |
names2 = getTestNames_unittest_collect(getLogPath('test_output_2017-04-25_11-01-16.unittest.log')) | |
# test_operators - after doctest fixes | |
#names2 = getTestNames_unittest_collect(getLogPath('test_output_2017-04-24_19-27-48.unittest.log')) | |
print len(names2) | |
set2 = set(names2) | |
print len(set2) == len(names2) | |
# all names - after doctest fixes | |
names3 = getTestNames_pytest_collect(getLogPath('output_pytest.log')) | |
# test_operators - after doctest fixes | |
#names3 = getTestNames_pytest_collect(getLogPath('output_pytest_operators.log')) | |
print len(names3) | |
set3 = set(names3) | |
print len(set3) == len(names3) | |
def getContents(path): | |
results = set() | |
contents = os.listdir(path) | |
for p in contents: | |
if p.endswith('.dist-info'): | |
continue | |
if p.endswith(('.pyc', '.pyo')): | |
p = p[:-4] | |
elif p.endswith('.py'): | |
p = p[:-3] | |
results.add(p) | |
return results | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment