summaryrefslogtreecommitdiff
path: root/tests/helpers.py
diff options
context:
space:
mode:
authorBryan Bishop <kanzure@gmail.com>2013-08-04 12:54:33 -0500
committerBryan Bishop <kanzure@gmail.com>2013-08-04 12:54:33 -0500
commit8d179bb81bba6de73669dd1643a9e7336d93c0a1 (patch)
tree9eaf4d76fb22b250f6fa3fef0bb7331c440dbee2 /tests/helpers.py
parentfac185b7814d5a47a33fef25d64e4ae94586670d (diff)
split meta-testing into separate files
Diffstat (limited to 'tests/helpers.py')
-rw-r--r--tests/helpers.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
new file mode 100644
index 0000000..16344a7
--- /dev/null
+++ b/tests/helpers.py
@@ -0,0 +1,92 @@
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import inspect
+from copy import copy
+import hashlib
+import random
+import json
+
+import unittest
+
+def assemble_test_cases():
+ """finds classes that inherit from unittest.TestCase
+ because i am too lazy to remember to add them to a
+ global list of tests for the suite runner"""
+ classes = []
+ clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
+ for (name, some_class) in clsmembers:
+ if issubclass(some_class, unittest.TestCase):
+ classes.append(some_class)
+ return classes
+
+def load_tests(loader, tests, pattern):
+ suite = unittest.TestSuite()
+ for test_class in assemble_test_cases():
+ tests = loader.loadTestsFromTestCase(test_class)
+ suite.addTests(tests)
+ return suite
+
+def check_has_test(func_name, tested_names):
+ """checks if there is a test dedicated to this function"""
+ if "test_"+func_name in tested_names:
+ return True
+ for name in tested_names:
+ if "test_"+func_name in name:
+ return True
+ return False
+
+def find_untested_methods():
+ """finds all untested functions in this module
+ by searching for method names in test case
+ method names."""
+ untested = []
+ avoid_funcs = ["main", "run_tests", "run_main", "copy", "deepcopy"]
+ test_funcs = []
+ # get a list of all classes in this module
+ classes = inspect.getmembers(sys.modules[__name__], inspect.isclass)
+ # for each class..
+ for (name, klass) in classes:
+ # only look at those that have tests
+ if issubclass(klass, unittest.TestCase):
+ # look at this class' methods
+ funcs = inspect.getmembers(klass, inspect.ismethod)
+ # for each method..
+ for (name2, func) in funcs:
+ # store the ones that begin with test_
+ if "test_" in name2 and name2[0:5] == "test_":
+ test_funcs.append([name2, func])
+ # assemble a list of all test method names (test_x, test_y, ..)
+ tested_names = [funcz[0] for funcz in test_funcs]
+ # now get a list of all functions in this module
+ funcs = inspect.getmembers(sys.modules[__name__], inspect.isfunction)
+ # for each function..
+ for (name, func) in funcs:
+ # we don't care about some of these
+ if name in avoid_funcs: continue
+ # skip functions beginning with _
+ if name[0] == "_": continue
+ # check if this function has a test named after it
+ has_test = check_has_test(name, tested_names)
+ if not has_test:
+ untested.append(name)
+ return untested
+
+def report_untested():
+ """
+ This reports about untested functions in the global namespace. This was
+ originally in the crystal module, where it would list out the majority of
+ the functions. Maybe it should be moved back.
+ """
+ untested = find_untested_methods()
+ output = "NOT TESTED: ["
+ first = True
+ for name in untested:
+ if first:
+ output += name
+ first = False
+ else: output += ", "+name
+ output += "]\n"
+ output += "total untested: " + str(len(untested))
+ return output