[hawkmoth] [PATCH 2/2] test: discover test cases based on output files

  • From: Bruno Santos <brunomanuelsantos@xxxxxxxxxxxxxxxxxx>
  • To: hawkmoth@xxxxxxxxxxxxx
  • Date: Fri, 1 Feb 2019 23:33:55 +0100

Test sources are not the ideal way to automatically discover tests
because every test requires a source file. This would require extra
metadata to discriminate which tests should be performed on each file,
which would be a needless maintenance burden.

Discovering tests based on the files used to check their success means
that sources can be easily shared across different test types and the
tests that are actually executed are in fact encoded in the file system
itself.

This scheme has the advantage of allowing test cases to be introduced in
a phased manner or opted out entirely in case they're somehow redundant.

All previous tests are still executed: so far only two types of tests
are supported and they're both related to the same .rst files.
---
 test/test_cautodoc.py | 8 ++++----
 test/test_hawkmoth.py | 7 ++++---
 test/testenv.py       | 7 +++----
 3 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/test/test_cautodoc.py b/test/test_cautodoc.py
index a1aff01..5c6063b 100755
--- a/test/test_cautodoc.py
+++ b/test/test_cautodoc.py
@@ -11,12 +11,12 @@
 
 @with_app(confdir=testenv.testdir, create_new_srcdir=True, buildername='text')
 def _get_output(input_filename, app, status, warning, **options):
-    shutil.copyfile(input_filename,
-                    testenv.modify_filename(input_filename, dir=app.srcdir))
+    source = testenv.modify_filename(input_filename, ext='c')
+    shutil.copyfile(source, testenv.modify_filename(source, dir=app.srcdir))
 
     with open(os.path.join(app.srcdir, 'index.rst'), 'w') as file:
         fmt = '.. c:autodoc:: {source}\n'
-        file.write(fmt.format(source=os.path.basename(input_filename)))
+        file.write(fmt.format(source=os.path.basename(source)))
         for key in [k for k in options.keys() if k in 
testenv.directive_options]:
             fmt = '   :{key}: {value}\n'
             file.write(fmt.format(key=key, value=options[key]))
@@ -37,7 +37,7 @@ def _get_expected(input_filename, app, status, warning, 
**options):
 class DirectiveTest(unittest.TestCase):
     pass
 
-testenv.assign_test_methods(DirectiveTest, _get_output, _get_expected)
+testenv.assign_test_methods(DirectiveTest, _get_output, _get_expected, '.rst')
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/test/test_hawkmoth.py b/test/test_hawkmoth.py
index 8d2b02b..b97edd3 100755
--- a/test/test_hawkmoth.py
+++ b/test/test_hawkmoth.py
@@ -9,15 +9,16 @@
 from hawkmoth import hawkmoth
 
 def _get_output(input_filename, **options):
-    return hawkmoth.parse_to_string(input_filename, False, **options)
+    source = testenv.modify_filename(input_filename, ext='c')
+    return hawkmoth.parse_to_string(source, False, **options)
 
 def _get_expected(input_filename, **options):
-    return testenv.read_file(input_filename, ext='rst')
+    return testenv.read_file(input_filename)
 
 class ParserTest(unittest.TestCase):
     pass
 
-testenv.assign_test_methods(ParserTest, _get_output, _get_expected)
+testenv.assign_test_methods(ParserTest, _get_output, _get_expected, '.rst')
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/test/testenv.py b/test/testenv.py
index b6842a8..b3f8b75 100644
--- a/test/testenv.py
+++ b/test/testenv.py
@@ -5,7 +5,6 @@
 import os
 import unittest
 
-testext = '.c'
 testdir = os.path.dirname(os.path.abspath(__file__))
 rootdir = os.path.dirname(testdir)
 
@@ -21,7 +20,7 @@ def _testcase_name(testcase):
 
     return name
 
-def get_testcases(path):
+def get_testcases(path, testext):
     for f in sorted(os.listdir(path)):
         if f.endswith(testext):
             yield os.path.join(path, f)
@@ -79,9 +78,9 @@ def test(self):
 
     return test
 
-def assign_test_methods(cls, get_output, get_expected):
+def assign_test_methods(cls, get_output, get_expected, testext):
     """Assign test case functions to the given class."""
-    for f in get_testcases(testdir):
+    for f in get_testcases(testdir, testext):
         options = get_testcase_options(f)
         method = _test_generator(get_output, get_expected, f, **options)
 
-- 
2.20.1


Other related posts: