[tor-commits] [stem/master] Addings support for testing single methods

atagar at torproject.org atagar at torproject.org
Tue Mar 3 17:32:13 UTC 2015


commit c9deaac9c29bcb37da4da6083d2e92bbb2819ef8
Author: Cristóbal <cristobal.leiva at usach.cl>
Date:   Mon Mar 2 14:04:09 2015 -0300

    Addings support for testing single methods
---
 run_tests.py |   38 ++++++++++++++++++++++----------------
 test/util.py |   41 ++++++++++++++++++++++++++---------------
 2 files changed, 48 insertions(+), 31 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index bfdd101..876162d 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -443,9 +443,9 @@ def _run_test(args, test_class, output_filters, logging_buffer):
   start_time = time.time()
 
   if args.verbose:
-    test.output.print_divider(test_class.__module__)
+    test.output.print_divider(test_class)
   else:
-    label = test_class.__module__
+    label = test_class
 
     if label.startswith('test.unit.'):
       label = label[10:]
@@ -457,23 +457,29 @@ def _run_test(args, test_class, output_filters, logging_buffer):
 
     println(label, STATUS, NO_NL)
 
-  suite = unittest.TestLoader().loadTestsFromTestCase(test_class)
+  suite = None
+  try:
+    suite = unittest.TestLoader().loadTestsFromName(test_class)
+  except:
+    println(' failed (%0.2fs)' % (time.time() - start_time), ERROR)
 
   test_results = StringIO()
-  run_result = unittest.TextTestRunner(test_results, verbosity=2).run(suite)
-
-  if args.verbose:
-    println(test.output.apply_filters(test_results.getvalue(), *output_filters))
-  elif not run_result.failures and not run_result.errors:
-    println(' success (%0.2fs)' % (time.time() - start_time), SUCCESS)
-  else:
-    if args.quiet:
-      println(label, STATUS, NO_NL, STDERR)
-      println(' failed (%0.2fs)' % (time.time() - start_time), ERROR, STDERR)
-      println(test.output.apply_filters(test_results.getvalue(), *output_filters), STDERR)
+  run_result = None
+
+  if suite:
+    run_result = unittest.TextTestRunner(test_results, verbosity=2).run(suite)
+    if args.verbose:
+      println(test.output.apply_filters(test_results.getvalue(), *output_filters))
+    elif not run_result.failures and not run_result.errors:
+      println(' success (%0.2fs)' % (time.time() - start_time), SUCCESS)
     else:
-      println(' failed (%0.2fs)' % (time.time() - start_time), ERROR)
-      println(test.output.apply_filters(test_results.getvalue(), *output_filters), NO_NL)
+      if args.quiet:
+        println(label, STATUS, NO_NL, STDERR)
+        println(' failed (%0.2fs)' % (time.time() - start_time), ERROR, STDERR)
+        println(test.output.apply_filters(test_results.getvalue(), *output_filters), STDERR)
+      else:
+        println(' failed (%0.2fs)' % (time.time() - start_time), ERROR)
+        println(test.output.apply_filters(test_results.getvalue(), *output_filters), NO_NL)
 
   test.output.print_logging(logging_buffer)
 
diff --git a/test/util.py b/test/util.py
index b6bca29..9a768d5 100644
--- a/test/util.py
+++ b/test/util.py
@@ -83,6 +83,8 @@ def get_unit_tests(module_substring = None):
 
   :returns: an **iterator** for our unit tests
   """
+  if module_substring and module_substring.startswith('test.unit.') == False:
+    module_substring = 'test.unit.' + module_substring
 
   return _get_tests(CONFIG['test.unit_tests'].splitlines(), module_substring)
 
@@ -95,31 +97,40 @@ def get_integ_tests(module_substring = None):
 
   :returns: an **iterator** for our integration tests
   """
+  if module_substring and module_substring.startswith('test.integ.') == False:
+    module_substring = 'test.integ.' + module_substring
 
   return _get_tests(CONFIG['test.integ_tests'].splitlines(), module_substring)
 
 
 def _get_tests(modules, module_substring):
+  # Look for module_substring in the list of all modules
+  modules_found = 0
   for import_name in modules:
     if import_name:
       if module_substring and module_substring not in import_name:
         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
+      modules_found += 1
+      yield import_name
+
+  # If no modules were found, then it might be that we were given
+  # a method (e.g test.integ.process.some_method).
+  # Delete the method substring and look again in the list of modules
+  if modules_found == 0:
+    module_list = module_substring.split('.')
+    # At this point all module_substring should be test.{integ|unit}.something
+    if(len(module_list) > 3):
+      module_substring = '.'.join(module_list[:-1])
+      class_method = module_list[-1]
+
+      for import_name in modules:
+        if import_name:
+          if module_substring and module_substring not in import_name:
+            continue
+
+          # If found, return module.method
+          yield import_name + '.' + class_method
 
 
 def get_help_message():





More information about the tor-commits mailing list