[tor-commits] [ooni-probe/master] [feature] Add setUpClass method to NetTestCase

art at torproject.org art at torproject.org
Mon May 30 16:28:32 UTC 2016


commit 9367f7f0398cfd0e2105596719c47009a5e65924
Author: Arturo Filastò <arturo at filasto.net>
Date:   Thu Apr 14 22:54:25 2016 +0200

    [feature] Add setUpClass method to NetTestCase
---
 ooni/director.py           |  2 +-
 ooni/nettest.py            | 18 +++++++++++++++++-
 ooni/tests/test_nettest.py |  6 ++++--
 3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/ooni/director.py b/ooni/director.py
index 64b5a98..43ca36c 100644
--- a/ooni/director.py
+++ b/ooni/director.py
@@ -257,7 +257,7 @@ class Director(object):
         net_test = NetTest(test_cases, test_details, report)
         net_test.director = self
 
-        yield net_test.initializeInputProcessor()
+        yield net_test.initialize()
         try:
             self.activeNetTests.append(net_test)
             self.measurementManager.schedule(net_test.generateMeasurements())
diff --git a/ooni/nettest.py b/ooni/nettest.py
index d01b3e4..67d3e27 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -566,14 +566,20 @@ class NetTest(object):
         return measurement
 
     @defer.inlineCallbacks
-    def initializeInputProcessor(self):
+    def initialize(self):
         for test_class, _ in self.testCases:
+            # Initialize Input Processor
             test_class.inputs = yield defer.maybeDeferred(
                 test_class().getInputProcessor
             )
             if not test_class.inputs:
                 test_class.inputs = [None]
 
+            # Run the setupClass method
+            yield defer.maybeDeferred(
+                test_class.setUpClass
+            )
+
     def generateMeasurements(self):
         """
         This is a generator that yields measurements and registers the
@@ -701,6 +707,16 @@ class NetTestCase(object):
 
     localOptions = {}
 
+    @classmethod
+    def setUpClass(cls):
+        """
+        You can override this hook with logic that should be run once before
+        any test method in the NetTestCase is run.
+        This can be useful to populate class attribute that should be valid
+        for all the runtime of the NetTest.
+        """
+        pass
+
     def _setUp(self):
         """
         This is the internal setup method to be overwritten by templates.
diff --git a/ooni/tests/test_nettest.py b/ooni/tests/test_nettest.py
index 91e663f..660fd3e 100644
--- a/ooni/tests/test_nettest.py
+++ b/ooni/tests/test_nettest.py
@@ -253,12 +253,13 @@ class TestNetTest(unittest.TestCase):
         ntl.loadNetTestString(net_test_string_with_required_option)
         self.assertRaises(MissingRequiredOption, ntl.checkOptions)
 
+    @defer.inlineCallbacks
     def test_net_test_inputs(self):
         ntl = NetTestLoader(dummyArgsWithFile)
         ntl.loadNetTestString(net_test_string_with_file)
         ntl.checkOptions()
         nt = NetTest(ntl.getTestCases(), ntl.getTestDetails(), None)
-        nt.initializeInputProcessor()
+        yield nt.initialize()
 
         # XXX: if you use the same test_class twice you will have consumed all
         # of its inputs!
@@ -275,6 +276,7 @@ class TestNetTest(unittest.TestCase):
         ntl.checkOptions()
         self.assertEqual(dict(ntl.localOptions), dummyOptions)
 
+    @defer.inlineCallbacks
     def test_generate_measurements_size(self):
         ntl = NetTestLoader(dummyArgsWithFile)
         ntl.loadNetTestString(net_test_string_with_file)
@@ -282,7 +284,7 @@ class TestNetTest(unittest.TestCase):
 
         net_test = NetTest(ntl.getTestCases(), ntl.getTestDetails(), None)
 
-        net_test.initializeInputProcessor()
+        yield net_test.initialize()
         measurements = list(net_test.generateMeasurements())
         self.assertEqual(len(measurements), 20)
 





More information about the tor-commits mailing list