calling the test dictionary from the constructor is effectively
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 24 Jul 2020 13:33:08 +0000 (14:33 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 24 Jul 2020 13:33:08 +0000 (14:33 +0100)
what unittest does.  this results in multiple copies of the test
being called (once by the dictionary-loop, once by unittest infrastructure)

getting the name of the actual test is a good thing.  used inspect.stack()
to find the name of the calling test

src/soc/fu/div/test/test_all_pipe_caller.py
src/soc/fu/div/test/test_pipe_caller.py

index 31bea464411962e8f2ea0d0d274dca9d5a9b1fba..86eb29d20c5555cea5b5de94ac8c557c3b2e2a59 100644 (file)
@@ -1,4 +1,5 @@
 import random
+import inspect
 import unittest
 from soc.simulator.program import Program
 from soc.config.endian import bigendian
@@ -11,14 +12,10 @@ class DivTestLong(unittest.TestCase):
     test_data = []
     def __init__(self, name):
         super().__init__(name)
-        for n, v in self.__class__.__dict__.items():
-            if n.startswith("test") and callable(v):
-                self._current_test_name = n
-                v(self)
 
     def run_test_program(self, prog, initial_regs=None, initial_sprs=None):
-        tc = TestCase(prog, self._current_test_name,
-                      initial_regs, initial_sprs)
+        test_name = inspect.stack()[1][3] # name of caller of this function
+        tc = TestCase(prog, test_name, initial_regs, initial_sprs)
         self.test_data.append(tc)
 
     def test_all(self):
index 573816fff614ee3d737934586f7eac16ceb1fded..9bb54e2389db5f0bc652844cca8eb28c9f9109ff 100644 (file)
@@ -1,3 +1,4 @@
+import inspect
 import random
 import unittest
 from nmutil.formaltest import FHDLTestCase
@@ -24,14 +25,9 @@ class DivTestCases(unittest.TestCase):
     def __init__(self, name):
         super().__init__(name)
 
-        for n, v in self.__class__.__dict__.items():
-            if n.startswith("test") and callable(v):
-                self._current_test_name = n
-                v(self)
-
     def run_test_program(self, prog, initial_regs=None, initial_sprs=None):
-        tc = TestCase(prog, self._current_test_name,
-                      initial_regs, initial_sprs)
+        test_name = inspect.stack()[1][3] # name of caller of this function
+        tc = TestCase(prog, test_name, initial_regs, initial_sprs)
         self.test_data.append(tc)
 
     def test_0_regression(self):