Skip to content

Commit

Permalink
Update test_examples.py to use subTest now that we have modern Pythons
Browse files Browse the repository at this point in the history
simplify it to use f-strings while we're at it
  • Loading branch information
eliben committed Jan 7, 2023
1 parent 85a4052 commit 4fffa23
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
sys.path.insert(0, '.')
from tests.test_util import run_exe, cpp_supported

EMIT_ELAPSED_TIME = False

# Runs all pycparser examples with no command-line arguments and makes sure they
# run successfully (return code = 0), without actually verifying their output.
class TestExamplesSucceed(unittest.TestCase):
Expand All @@ -16,19 +14,11 @@ def test_all_examples(self):
root = './examples'
for filename in os.listdir(root):
if os.path.splitext(filename)[1] == '.py':
# TODO: It would be nice to use subTest here, but that's not
# available in Python 2.7
# Use it when we finally drop Python 2...
path = os.path.join(root, filename)
t1 = time.time()
rc, stdout, stderr = run_exe(path)
elapsed = time.time() - t1
if EMIT_ELAPSED_TIME:
print('{}... elapsed: {}'.format(filename, elapsed))
self.assertEqual(
rc, 0, 'example "{}" failed with stdout =\n{}\nstderr =\n{}'.format(filename, stdout, stderr))

with self.subTest(name=filename):
path = os.path.join(root, filename)
rc, stdout, stderr = run_exe(path)
self.assertEqual(
rc, 0, f'example "{filename}" failed with stdout =\n{stdout}\nstderr =\n{stderr}')

if __name__ == '__main__':
EMIT_ELAPSED_TIME = True
unittest.main()

0 comments on commit 4fffa23

Please sign in to comment.