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