[tor-commits] [stem/master] Moving test imports to the runner module

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


commit 76e751f9c8fd338c73752a8547cedc92a95363de
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Apr 10 10:16:35 2013 -0700

    Moving test imports to the runner module
    
    Ideally the run_tests.py module should be as simple as possible, simply parsing
    the user input and calling helper functions to run the tests. To this end I'm
    moving the code for enumerating unit and integration tests into the runner
    module.
---
 run_tests.py   |   33 ++-------------------------------
 test/runner.py |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 31 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 17de0ee..096de69 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -45,8 +45,6 @@ CONFIG = stem.util.conf.config_dict("test", {
   "target.prereq": {},
   "target.torrc": {},
   "integ.test_directory": "./test/data",
-  "test.unit_tests": "",
-  "test.integ_tests": "",
 })
 
 Target = stem.util.enum.UppercaseEnum(
@@ -284,23 +282,6 @@ def _print_style_issues():
       print
 
 
-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
-
 if __name__ == '__main__':
   try:
     stem.prereq.check_requirements()
@@ -377,12 +358,7 @@ if __name__ == '__main__':
     test.output.print_divider("UNIT TESTS", True)
     error_tracker.set_category("UNIT TEST")
 
-    for test_module in CONFIG["test.unit_tests"].splitlines():
-      if not test_module:
-        continue
-
-      test_class = _import_test(test_module)
-
+    for test_class in test.runner.get_unit_tests():
       if CONFIG["argument.test"] and \
         not test_class.__module__.startswith(CONFIG["argument.test"]):
         continue
@@ -466,12 +442,7 @@ if __name__ == '__main__':
         test.output.print_line("Running tests...", term.Color.BLUE, term.Attr.BOLD)
         print
 
-        for test_module in CONFIG["test.integ_tests"].splitlines():
-          if not test_module:
-            continue
-
-          test_class = _import_test(test_module)
-
+        for test_class in test.runner.get_integ_tests():
           if CONFIG["argument.test"] and \
             not test_class.__module__.startswith(CONFIG["argument.test"]):
             continue
diff --git a/test/runner.py b/test/runner.py
index 5010224..592d367 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -18,6 +18,9 @@ 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,6 +68,8 @@ CONFIG = stem.util.conf.config_dict("test", {
   "integ.target.online": False,
   "integ.target.relative_data_dir": False,
   "integ.target.chroot": False,
+  "test.unit_tests": "",
+  "test.integ_tests": "",
 })
 
 STATUS_ATTR = (term.Color.BLUE, term.Attr.BOLD)
@@ -211,6 +216,49 @@ def exercise_controller(test_case, controller):
   test_case.assertEquals("config-file=%s\nOK" % torrc_path, str(config_file_response))
 
 
+
+def get_unit_tests():
+  """
+  Provides the classes for our unit tests.
+
+  :returns: an **iterator** for our unit tests
+  """
+
+  for test_module in CONFIG["test.unit_tests"].splitlines():
+    if test_module:
+      yield _import_test(test_module)
+
+
+def get_integ_tests():
+  """
+  Provides the classes for our integration tests.
+
+  :returns: an **iterator** for our integration tests
+  """
+
+  for test_module in CONFIG["test.unit_tests"].splitlines():
+    if test_module:
+      yield _import_test(test_module)
+
+
+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.





More information about the tor-commits mailing list