[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Refactor test_case loader so that test methods share an input set



commit e07558f035ee50e597d30ee82f1d4f3b7c25aabd
Author: aagbsn <aagbsn@xxxxxxxx>
Date:   Sat Jan 26 11:49:57 2013 +0000

    Refactor test_case loader so that test methods share an input set
---
 ooni/nettest.py       |   21 +++++++++++----------
 tests/test_nettest.py |   44 +++++++++++++-------------------------------
 2 files changed, 24 insertions(+), 41 deletions(-)

diff --git a/ooni/nettest.py b/ooni/nettest.py
index 61e6270..9e86ab4 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -202,8 +202,11 @@ class NetTestLoader(object):
         try:
             assert issubclass(item, NetTestCase)
             methods = reflect.prefixedMethodNames(item, self.method_prefix)
+            test_methods = []
             for method in methods:
-                test_cases.append((item, self.method_prefix + method))
+                test_methods.append(self.method_prefix + method)
+            if test_methods:
+                test_cases.append((item, test_methods))
         except (TypeError, AssertionError):
             pass
         return test_cases
@@ -336,15 +339,13 @@ class NetTest(object):
         This is a generator that yields measurements and registers the
         callbacks for when a measurement is successful or has failed.
         """
-        self.report.open()
-        for test_class, test_method in self.testCases:
-            log.debug("Running %s %s" % (test_class, test_method))
-            for test_input in test_class.inputs:
-                measurement = self.makeMeasurement(test_class, test_method,
-                        test_input)
-
-                self.state.taskCreated()
-                yield measurement
+        for test_class, test_methods in self.testCases:
+            for input in test_class.inputs:
+                for method in test_methods:
+                    log.debug("Running %s %s" % (test_class, method))
+                    measurement = self.makeMeasurement(test_class, method, input)
+                    self.state.taskCreated()
+                    yield measurement
 
         self.state.allTasksScheduled()
 
diff --git a/tests/test_nettest.py b/tests/test_nettest.py
index ee571e7..5645dbf 100644
--- a/tests/test_nettest.py
+++ b/tests/test_nettest.py
@@ -104,6 +104,16 @@ class TestNetTest(unittest.TestCase):
     def assertCallable(self, thing):
         self.assertIn('__call__', dir(thing))
 
+    def verifyMethods(self, testCases):
+        uniq_test_methods = set()
+        for test_class, test_methods in testCases:
+            instance = test_class()
+            for test_method in test_methods:
+                c = getattr(instance, test_method)
+                self.assertCallable(c)
+                uniq_test_methods.add(test_method)
+        self.assertEqual(set(['test_a', 'test_b']), uniq_test_methods)
+
     def test_load_net_test_from_file(self):
         """
         Given a file verify that the net test cases are properly
@@ -116,17 +126,7 @@ class TestNetTest(unittest.TestCase):
 
         ntl = NetTestLoader(net_test_file,
                 dummyArgs)
-
-        test_methods = set()
-        for test_class, test_method in ntl.testCases:
-            instance = test_class()
-            c = getattr(instance, test_method)
-            self.assertCallable(c)
-
-            test_methods.add(test_method)
-
-        self.assertEqual(set(['test_a', 'test_b']), test_methods)
-
+        self.verifyMethods(ntl.testCases)
         os.unlink(net_test_file)
 
     def test_load_net_test_from_str(self):
@@ -136,16 +136,7 @@ class TestNetTest(unittest.TestCase):
         """
         ntl = NetTestLoader(net_test_string,
                 dummyArgs)
-
-        test_methods = set()
-        for test_class, test_method in ntl.testCases:
-            instance = test_class()
-            c = getattr(instance, test_method)
-            self.assertCallable(c)
-
-            test_methods.add(test_method)
-
-        self.assertEqual(set(['test_a', 'test_b']), test_methods)
+        self.verifyMethods(ntl.testCases)
 
     def test_load_net_test_from_StringIO(self):
         """
@@ -154,16 +145,7 @@ class TestNetTest(unittest.TestCase):
         """
         ntl = NetTestLoader(StringIO(net_test_string),
                 dummyArgs)
-
-        test_methods = set()
-        for test_class, test_method in ntl.testCases:
-            instance = test_class()
-            c = getattr(instance, test_method)
-            self.assertCallable(c)
-
-            test_methods.add(test_method)
-
-        self.assertEqual(set(['test_a', 'test_b']), test_methods)
+        self.verifyMethods(ntl.testCases)
 
     def test_load_with_option(self):
         ntl = NetTestLoader(StringIO(net_test_string),



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits