Belle II Software  release-05-02-19
examples.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import os
5 import sys
6 import subprocess
7 import unittest
8 import glob
9 from basf2 import find_file
10 from b2test_utils import clean_working_directory, skip_test_if_light
11 
12 
13 class ExamplesTest(unittest.TestCase):
14  """Test to run all example scripts."""
15 
16  @unittest.skipIf(not os.getenv('BELLE2_EXAMPLES_DATA_DIR'),
17  "$BELLE2_EXAMPLES_DATA_DIR not found.")
18  @unittest.skipIf(not os.getenv('BELLE2_VALIDATION_DATA_DIR'),
19  "$BELLE2_VALIDATION_DATA_DIR not found.")
20  def _test_examples_dir(self, path_to_glob, broken=[]):
21  """
22  Internal function to test a directory full of example scripts with an optional list of broken scripts to be skipped.
23 
24  Parameters:
25  path_to_glob (str): the path to search for scripts
26  broken (list(str)): (optional) scripts that are known to be broken and can be skipped
27  """
28  all_egs = sorted(glob.glob(find_file(path_to_glob) + "*.py"))
29  for eg in all_egs:
30  filename = os.path.basename(eg)
31  if filename not in broken:
32  with self.subTest(msg=filename):
33  result = subprocess.run(['basf2', '-n1', eg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
34  if result.returncode != 0:
35  # failure running example so let's print the output
36  # on stderr so it's not split from output of unittest
37  # done like this since we don't want to decode/encode utf8
38  sys.stdout.buffer.write(result.stdout)
39  self.assertEqual(result.returncode, 0)
40 
42  """
43  Test supported calibration examples.
44  """
45  skip_test_if_light(self) # calibration dataobjects not supported in light releases
46 
47  # list of the broken examples (to be removed when they are individually fixed)
48  broken_cal_egs = ['B2CAL901-cDSTECLTRG.py' # BII-4276
49  ]
50 
51  self._test_examples_dir('analysis/examples/calibration/', broken_cal_egs)
52 
53  def test_fei_examples(self):
54  """
55  Test supported FEI examples.
56  """
57 
58  self._test_examples_dir('analysis/examples/FEI/')
59 
61  """
62  Test supported fitting examples.
63  """
64 
65  self._test_examples_dir('analysis/examples/fitting/')
66 
67  def test_mva_examples(self):
68  """
69  Test supported mva examples.
70  """
71  # list of the broken examples (to be removed when they are individually fixed)
72  broken_mva_egs = ['B2A711-DeepContinuumSuppression_Input.py', # BII-4279
73  'B2A713-DeepContinuumSuppression_MVAExpert.py', # BII-4279
74  'B2A712-DeepContinuumSuppression_MVATrain.py', # BII-4279
75  'B2A714-DeepContinuumSuppression_MVAModel.py' # BII-4279
76  ]
77 
78  self._test_examples_dir('analysis/examples/mva/', broken_mva_egs)
79 
81  """
82  Test supported reconstruction examples.
83  """
84 
85  self._test_examples_dir('analysis/examples/reconstruction/')
86 
88  """
89  Test supported simulation examples.
90  """
91  skip_test_if_light(self) # simulation doesn't work in light releaes
92 
93  # list of the broken examples (to be removed when they are individually fixed)
94  broken_sim_egs = ['B2A104-SimulateAndReconstruct-withBeamBkg.py'
95  ]
96 
97  self._test_examples_dir('analysis/examples/simulations/', broken_sim_egs)
98 
100  """
101  Test supported tagging examples.
102  """
103 
104  self._test_examples_dir('analysis/examples/tagging/')
105 
107  """
108  Test supported variable manager examples.
109  """
110 
111  self._test_examples_dir('analysis/examples/VariableManager/')
112 
114  """
115  Test supported PostMdstIdentification examples.
116  """
117 
118  self._test_examples_dir('analysis/examples/PostMdstIdentification/')
119 
120 
121 if __name__ == '__main__':
122  with clean_working_directory():
123  unittest.main()
examples.ExamplesTest
Definition: examples.py:13
examples.ExamplesTest.test_simulation_examples
def test_simulation_examples(self)
Definition: examples.py:87
examples.ExamplesTest.test_variablemanager_examples
def test_variablemanager_examples(self)
Definition: examples.py:106
examples.ExamplesTest.test_calibration_examples
def test_calibration_examples(self)
Definition: examples.py:41
examples.ExamplesTest.test_postmdstidentification_examples
def test_postmdstidentification_examples(self)
Definition: examples.py:113
examples.ExamplesTest.test_tagging_examples
def test_tagging_examples(self)
Definition: examples.py:99
examples.ExamplesTest.test_reconstruction_examples
def test_reconstruction_examples(self)
Definition: examples.py:80
examples.ExamplesTest.test_fei_examples
def test_fei_examples(self)
Definition: examples.py:53
examples.ExamplesTest.test_mva_examples
def test_mva_examples(self)
Definition: examples.py:67
examples.ExamplesTest.test_fitting_examples
def test_fitting_examples(self)
Definition: examples.py:60
examples.ExamplesTest._test_examples_dir
def _test_examples_dir(self, path_to_glob, broken=[])
Definition: examples.py:20