[tor-commits] [stem/master] Moving funcions for getting tests into util

atagar at torproject.org atagar at torproject.org
Sun Apr 14 04:33:47 UTC 2013


commit 49c55af78b8adf699f934bcd48248539acc83d6b
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Apr 12 08:57:50 2013 -0700

    Moving funcions for getting tests into util
    
    Now that we've freed up the util namespace I'm gonna gobble it up again for
    testing framework helpers. Hopefully this will be able to replace quite a few
    of our functions.
---
 run_tests.py   |    5 ++-
 test/runner.py |   61 -------------------------------------------------------
 test/util.py   |   62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 63 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 65c3444..05f409c 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -24,6 +24,7 @@ from stem.util import log, system, term
 import test.output
 import test.runner
 import test.static_checks
+import test.util
 
 from test.runner import Target
 
@@ -328,7 +329,7 @@ if __name__ == '__main__':
     test.output.print_divider("UNIT TESTS", True)
     error_tracker.set_category("UNIT TEST")
 
-    for test_class in test.runner.get_unit_tests(test_prefix):
+    for test_class in test.util.get_unit_tests(test_prefix):
       test.output.print_divider(test_class.__module__)
       suite = unittest.TestLoader().loadTestsFromTestCase(test_class)
       test_results = StringIO.StringIO()
@@ -384,7 +385,7 @@ if __name__ == '__main__':
         test.output.print_line("Running tests...", term.Color.BLUE, term.Attr.BOLD)
         print
 
-        for test_class in test.runner.get_integ_tests(test_prefix):
+        for test_class in test.util.get_integ_tests(test_prefix):
           test.output.print_divider(test_class.__module__)
           suite = unittest.TestLoader().loadTestsFromTestCase(test_class)
           test_results = StringIO.StringIO()
diff --git a/test/runner.py b/test/runner.py
index fb30c9f..aa4e4a9 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -18,9 +18,6 @@ about the tor test instance they're running against.
   require_online - skips unless targets allow for online tests
   exercise_controller - basic sanity check that a controller connection can be used
 
-  get_unit_tests - provides our unit tests
-  get_integ_tests - provides our integration tests
-
   get_runner - Singleton for fetching our runtime context.
   Runner - Runtime context for our integration tests.
     |- start - prepares and starts a tor instance for our tests to run against
@@ -65,8 +62,6 @@ from stem.util import term
 CONFIG = stem.util.conf.config_dict("test", {
   "integ.test_directory": "./test/data",
   "integ.log": "./test/data/log",
-  "test.unit_tests": "",
-  "test.integ_tests": "",
 })
 
 Target = stem.util.enum.UppercaseEnum(
@@ -227,62 +222,6 @@ def exercise_controller(test_case, controller):
   test_case.assertEquals("config-file=%s\nOK" % torrc_path, str(config_file_response))
 
 
-def get_unit_tests(prefix = None):
-  """
-  Provides the classes for our unit tests.
-
-  :param str prefix: only provide the test if the module starts with this prefix
-
-  :returns: an **iterator** for our unit tests
-  """
-
-  for line in CONFIG["test.unit_tests"].splitlines():
-    if line:
-      test_class = _import_test(line)
-
-      if prefix and not test_class.__module__.startswith(prefix):
-        continue
-
-      yield test_class
-
-
-def get_integ_tests(prefix = None):
-  """
-  Provides the classes for our integration tests.
-
-  :param str prefix: only provide the test if the module starts with this prefix
-
-  :returns: an **iterator** for our integration tests
-  """
-
-  for line in CONFIG["test.integ_tests"].splitlines():
-    if line:
-      test_class = _import_test(line)
-
-      if prefix and not test_class.__module__.startswith(prefix):
-        continue
-
-      yield test_class
-
-
-def _import_test(import_name):
-  # Dynamically imports test modules. The __import__() call has a couple quirks
-  # that make this a little clunky...
-  #
-  #   * it only accepts modules, not the actual class we want to import
-  #
-  #   * it returns the top level module, so we need to transverse into it for
-  #     the test class
-
-  module_name = '.'.join(import_name.split('.')[:-1])
-  module = __import__(module_name)
-
-  for subcomponent in import_name.split(".")[1:]:
-    module = getattr(module, subcomponent)
-
-  return module
-
-
 def get_runner():
   """
   Singleton for the runtime context of integration tests.
diff --git a/test/util.py b/test/util.py
new file mode 100644
index 0000000..8ce61bf
--- /dev/null
+++ b/test/util.py
@@ -0,0 +1,62 @@
+"""
+Helper functions for our test framework.
+
+::
+
+  get_unit_tests - provides our unit tests
+  get_integ_tests - provides our integration tests
+"""
+
+import stem.util.conf
+
+CONFIG = stem.util.conf.config_dict("test", {
+  "test.unit_tests": "",
+  "test.integ_tests": "",
+})
+
+
+def get_unit_tests(prefix = None):
+  """
+  Provides the classes for our unit tests.
+
+  :param str prefix: only provide the test if the module starts with this prefix
+
+  :returns: an **iterator** for our unit tests
+  """
+
+  return _get_tests(CONFIG["test.unit_tests"].splitlines(), prefix)
+
+
+def get_integ_tests(prefix = None):
+  """
+  Provides the classes for our integration tests.
+
+  :param str prefix: only provide the test if the module starts with this prefix
+
+  :returns: an **iterator** for our integration tests
+  """
+
+  return _get_tests(CONFIG["test.integ_tests"].splitlines(), prefix)
+
+
+def _get_tests(modules, prefix):
+  for import_name in modules:
+    if import_name:
+      if prefix and not import_name.startswith(prefix):
+        continue
+
+      # Dynamically imports test modules. The __import__() call has a couple
+      # quirks that make this a little clunky...
+      #
+      #   * it only accepts modules, not the actual class we want to import
+      #
+      #   * it returns the top level module, so we need to transverse into it
+      #     for the test class
+
+      module_name = '.'.join(import_name.split('.')[:-1])
+      module = __import__(module_name)
+
+      for subcomponent in import_name.split(".")[1:]:
+        module = getattr(module, subcomponent)
+
+      yield module





More information about the tor-commits mailing list