Belle II Software  light-2303-iriomote
tutorials_B2A2XX.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import os
13 import sys
14 import subprocess
15 import unittest
16 import glob
17 from basf2 import find_file
18 from b2test_utils import clean_working_directory, configure_logging_for_tests
19 from b2test_utils_analysis import scanTTree
20 
21 
22 class TutorialsTest(unittest.TestCase):
23  """Test to run all B2A2XX tutorials. Will fail if no tutorial directory is found."""
24 
25 
26  broken_tutorials = []
27 
28  @unittest.skipIf(not os.getenv('BELLE2_EXAMPLES_DATA_DIR'),
29  "$BELLE2_EXAMPLES_DATA_DIR not found.")
30  @unittest.skipIf(not os.getenv('BELLE2_VALIDATION_DATA_DIR'),
31  "$BELLE2_VALIDATION_DATA_DIR not found.")
32  def test_tutorials(self):
33  """
34  Test supported tutorials.
35  """
36  configure_logging_for_tests()
37  all_tutorials = sorted(glob.glob(find_file('analysis/examples/tutorials/') + "/B2A2*.py"))
38  for tutorial in all_tutorials:
39  filename = os.path.basename(tutorial)
40  if filename not in self.broken_tutorialsbroken_tutorials:
41  with self.subTest(msg=filename):
42  outputfilename = filename.replace('.py', '.root')
43  result = subprocess.run(['basf2', '-n100', tutorial, '-o', outputfilename],
44  stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
45  if result.returncode != 0:
46  # failure running tutorial so let's print the output
47  # on stderr so it's not split from output of unittest
48  # done like this since we don't want to decode/encode utf8
49  sys.stdout.buffer.write(result.stdout)
50  self.assertEqual(result.returncode, 0)
51 
52  if os.path.exists(outputfilename):
53  scanTTree(outputfilename)
54 
55 
56 if __name__ == '__main__':
57  with clean_working_directory():
58  unittest.main()
list broken_tutorials
list of the broken tutorials (to be removed when they are individually fixed)