VTL: Allow running simple unittest.TestCases. 95/16795/12
authorPaul Vinciguerra <pvinci@vinciconsulting.com>
Mon, 14 Jan 2019 00:09:10 +0000 (16:09 -0800)
committerOle Trøan <otroan@employees.org>
Mon, 14 Jan 2019 13:09:08 +0000 (13:09 +0000)
It came to my attention that Ole added a simple test in:
https://gerrit.fd.io/r/#/c/16381/ and the framework forced him
to launch an instance of VPP to test the formatting of a mac address.

This change allows the test framework to run standard unittest.TestCases
without the need to spawn a VPP instance.

Change-Id: I56651ab27c4c6bf920081a526f168a743d643201
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
test/framework.py
test/test_syslog.py
test/test_util.py

index 604e342..3098d39 100644 (file)
@@ -272,14 +272,6 @@ class VppTestCase(unittest.TestCase):
 
         return random.choice(tuple(min_usage_set))
 
-    @classmethod
-    def print_header(cls):
-        if not hasattr(cls, '_header_printed'):
-            print(double_line_delim)
-            print(colorize(getdoc(cls).splitlines()[0], GREEN))
-            print(double_line_delim)
-            cls._header_printed = True
-
     @classmethod
     def setUpConstants(cls):
         """ Set-up the test case class based on environment variables """
@@ -401,7 +393,6 @@ class VppTestCase(unittest.TestCase):
         """
         gc.collect()  # run garbage collection first
         random.seed()
-        cls.print_header()
         cls.logger = get_logger(cls.__name__)
         if hasattr(cls, 'parallel_handler'):
             cls.logger.addHandler(cls.parallel_handler)
@@ -1051,7 +1042,7 @@ class VppTestResult(unittest.TestResult):
             test case descriptions.
         :param verbosity Integer variable to store required verbosity level.
         """
-        unittest.TestResult.__init__(self, stream, descriptions, verbosity)
+        super(VppTestResult, self).__init__(stream, descriptions, verbosity)
         self.stream = stream
         self.descriptions = descriptions
         self.verbosity = verbosity
@@ -1209,7 +1200,15 @@ class VppTestResult(unittest.TestResult):
         :param test:
 
         """
-        test.print_header()
+
+        def print_header(test):
+            if not hasattr(test.__class__, '_header_printed'):
+                print(double_line_delim)
+                print(colorize(getdoc(test).splitlines()[0], GREEN))
+                print(double_line_delim)
+            test.__class__._header_printed = True
+
+        print_header(test)
 
         unittest.TestResult.startTest(self, test)
         if self.verbosity > 0:
@@ -1345,3 +1344,6 @@ class Worker(Thread):
         self.logger.info(err)
         self.logger.info(single_line_delim)
         self.result = self.process.returncode
+
+if __name__ == '__main__':
+    pass
index 9b328be..b407bdf 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import unittest
 from framework import VppTestCase, VppTestRunner
 from util import ppp
 from scapy.packet import Raw
index 01ba862..e7d5366 100755 (executable)
@@ -1,12 +1,12 @@
 #!/usr/bin/env python
-"""Test framework utilitty functions tests"""
+"""Test framework utility functions tests"""
 
 import unittest
-from framework import VppTestCase, VppTestRunner
+from framework import VppTestRunner
 from vpp_papi import mac_pton, mac_ntop
 
 
-class TestUtil (VppTestCase):
+class TestUtil (unittest.TestCase):
     """ MAC to binary and back """
     def test_mac_to_binary(self):
         mac = 'aa:bb:cc:dd:ee:ff'
@@ -15,5 +15,6 @@ class TestUtil (VppTestCase):
         self.assertEqual(type(mac), type(mac2))
         self.assertEqual(mac2, mac)
 
+
 if __name__ == '__main__':
     unittest.main(testRunner=VppTestRunner)