[tor-commits] [stem/master] Helper function to only run tests once

atagar at torproject.org atagar at torproject.org
Thu May 17 16:32:28 UTC 2012


commit b69db7feba640967c2bb6f2740d0aac2bf32666f
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu May 17 09:19:28 2012 -0700

    Helper function to only run tests once
    
    Adding a helper function to skip tests if they've been run before. This is
    needed because some of our tests take a long time to run and are uneffected by
    testing targets, so there's no point in wasting time on running them
    repeatedly.
---
 test/integ/descriptor/extrainfo_descriptor.py |   11 ++++-------
 test/integ/descriptor/server_descriptor.py    |   14 ++++----------
 test/runner.py                                |   17 +++++++++++++++++
 3 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/test/integ/descriptor/extrainfo_descriptor.py b/test/integ/descriptor/extrainfo_descriptor.py
index ea58807..479ff01 100644
--- a/test/integ/descriptor/extrainfo_descriptor.py
+++ b/test/integ/descriptor/extrainfo_descriptor.py
@@ -10,8 +10,6 @@ import stem.descriptor.extrainfo_descriptor
 import test.runner
 import test.integ.descriptor
 
-RAN_CACHED_DESCRIPTOR_TEST = False
-
 class TestExtraInfoDescriptor(unittest.TestCase):
   def test_metrics_descriptor(self):
     """
@@ -68,15 +66,14 @@ k0d2aofcVbHr4fPQOSST0LXDrhFl5Fqo5um296zpJGvRUeO6S44U/EfJAGShtqWw
     additions.
     """
     
-    global RAN_CACHED_DESCRIPTOR_TEST
+    # lengthy test and uneffected by targets, so only run once
+    test.runner.only_run_once(self, "test_cached_descriptor")
+    
     descriptor_path = test.runner.get_runner().get_test_dir("cached-extrainfo")
     
-    if RAN_CACHED_DESCRIPTOR_TEST:
-      self.skipTest("(already ran)")
-    elif not os.path.exists(descriptor_path):
+    if not os.path.exists(descriptor_path):
       self.skipTest("(no cached descriptors)")
     
-    RAN_CACHED_DESCRIPTOR_TEST = True
     with open(descriptor_path) as descriptor_file:
       for desc in stem.descriptor.extrainfo_descriptor.parse_file(descriptor_file):
         unrecognized_lines = desc.get_unrecognized_lines()
diff --git a/test/integ/descriptor/server_descriptor.py b/test/integ/descriptor/server_descriptor.py
index b5664c4..5d7e62d 100644
--- a/test/integ/descriptor/server_descriptor.py
+++ b/test/integ/descriptor/server_descriptor.py
@@ -12,11 +12,6 @@ import stem.descriptor.server_descriptor
 import test.runner
 import test.integ.descriptor
 
-# 'test_cached_descriptor' is a lengthy test and uneffected by testing targets,
-# so including a flag to prevent it from being ran multiple times
-
-RAN_CACHED_DESCRIPTOR_TEST = False
-
 class TestServerDescriptor(unittest.TestCase):
   def test_metrics_descriptor(self):
     """
@@ -146,15 +141,14 @@ Qlx9HNCqCY877ztFRC624ja2ql6A2hBcuoYMbkHjcQ4=
     additions.
     """
     
-    global RAN_CACHED_DESCRIPTOR_TEST
+    # lengthy test and uneffected by targets, so only run once
+    test.runner.only_run_once(self, "test_cached_descriptor")
+    
     descriptor_path = test.runner.get_runner().get_test_dir("cached-descriptors")
     
-    if RAN_CACHED_DESCRIPTOR_TEST:
-      self.skipTest("(already ran)")
-    elif not os.path.exists(descriptor_path):
+    if not os.path.exists(descriptor_path):
       self.skipTest("(no cached descriptors)")
     
-    RAN_CACHED_DESCRIPTOR_TEST = True
     with open(descriptor_path) as descriptor_file:
       for desc in stem.descriptor.server_descriptor.parse_file(descriptor_file):
         # the following attributes should be deprecated, and not appear in the wild
diff --git a/test/runner.py b/test/runner.py
index 5ee9098..2dc0cbb 100644
--- a/test/runner.py
+++ b/test/runner.py
@@ -86,6 +86,9 @@ Torrc = stem.util.enum.Enum(
   ("PTRACE", "DisableDebuggerAttachment 0"),
 )
 
+# (test_instance, test_name) tuples that we've registered as having been ran
+RAN_TESTS = []
+
 class RunnerStopped(Exception):
   "Raised when we try to use a Runner that doesn't have an active tor instance"
   pass
@@ -117,6 +120,20 @@ def require_version(test_case, req_version):
   if get_runner().get_tor_version() < req_version:
     test_case.skipTest("(requires %s)" % req_version)
 
+def only_run_once(test_case, test_name):
+  """
+  Skips the test if it has ran before. If it hasn't then flags it as being ran.
+  
+  Arguments:
+    test_case (unittest.TestCase) - test being ran
+    test_name (str) - name of the test being ran
+  """
+  
+  if (test_case, test_name) in RAN_TESTS:
+    test_case.skipTest("(already ran)")
+  else:
+    RAN_TESTS.append((test_case, test_name))
+
 def exercise_controller(test_case, controller):
   """
   Checks that we can now use the socket by issuing a 'GETINFO config-file'





More information about the tor-commits mailing list