4 """ Extended version of analysis/test/examples.py. """
12 from typing
import Optional, List
15 from basf2
import find_file
16 from b2test_utils
import clean_working_directory
19 def skip_expensive_tests() -> bool:
20 """ Returns true if we want to skip more expensive tests, e.g. on
23 return os.environ.get(
"SKIP_EXPENSIVE_TESTS",
"no").lower()
in [
32 """ Test steering files """
37 broken: Optional[List[str]] =
None,
38 additional_arguments: Optional[List[str]] =
None,
39 expensive_tests: Optional[List[str]] =
None,
40 change_working_directory=
True,
43 Internal function to test a directory full of example scripts with an
44 optional list of broken scripts to be skipped.
47 path_to_glob (str): the path to a directory to search for python
48 scripts (must end in .py)
49 broken (list(str)): (optional) names of scripts that are known to
50 be broken and can be skipped
51 expensive_tests (list(str)): (optional) names of scripts that take
52 longer and should e.g. not run on bamboo
53 additional_arguments (list(str)): (optional) additional arguments
54 for basf2 to be passed when testing the scripts
55 change_working_directory: Change to path_to_glob for execution
57 if additional_arguments
is None:
58 additional_arguments = []
61 if expensive_tests
is None:
63 working_dir = find_file(path_to_glob)
64 all_egs = sorted(glob.glob(working_dir +
"/*.py"))
66 filename = os.path.basename(eg)
67 if filename
in broken:
69 if skip_expensive_tests
and filename
in expensive_tests:
71 with self.subTest(msg=filename):
72 result = subprocess.run(
73 [
"basf2",
"-n1", eg, *additional_arguments],
74 stdout=subprocess.PIPE,
75 stderr=subprocess.STDOUT,
78 if result.returncode != 0:
82 sys.stdout.buffer.write(result.stdout)
83 self.assertEqual(result.returncode, 0)
90 "starterkit/2021/1111540100_eph3_BGx0_1.root",
95 "Test data files not found.",
97 def test_lessons_1_to_5(self):
99 path_to_glob=
"online_book/basf2/steering_files",
100 additional_arguments=[
"1"],
102 "065_generate_mc.py",
105 change_working_directory=
True,
109 if __name__ ==
"__main__":
110 with clean_working_directory():