The Gaudi Framework  v38r0 (2143aa4c)
collect_for_ctest.py
Go to the documentation of this file.
1 
11 """
12 pytest plugin that report collected pytest files as CTest tests
13 
14 This plugin is not meant to be used directly, but it is invoked by the
15 CMake function `gaudi_add_pytest()`
16 """
17 import argparse
18 import os
19 from pathlib import Path
20 
21 
22 def pytest_addoption(parser, pluginmanager):
23  parser.addoption(
24  "--ctest-output-file",
25  type=argparse.FileType("w"),
26  help="name of the file to write to communicate to ctest the discovered tests",
27  )
28  parser.addoption(
29  "--ctest-pytest-command",
30  default="pytest",
31  help="how pytest has to be invoked (e.g. using wrapper commands)",
32  )
33  parser.addoption(
34  "--ctest-pytest-root-dir",
35  default=Path.cwd(),
36  help="root directory to compute test names",
37  )
38  parser.addoption(
39  "--ctest-prefix",
40  default="",
41  help="string to prefix to the generated test names",
42  )
43  parser.addoption(
44  "--ctest-label",
45  default=["pytest"],
46  action="append",
47  help="labels to attach to the test (the label pytest is always added)",
48  )
49  parser.addoption(
50  "--ctest-properties",
51  default=[],
52  action="append",
53  help="test properties to set for all discovered tests",
54  )
55 
56 
57 def pytest_collection_modifyitems(session, config, items):
58  args = {
59  name[6:]: getattr(config.option, name)
60  for name in dir(config.option)
61  if name.startswith("ctest_")
62  }
63  session.ctest_args = args
64  session.ctest_files = set(
65  item.path if hasattr(item, "path") else Path(item.fspath) for item in items
66  )
67 
68 
69 TEST_DESC_TEMPLATE = """
70 add_test({name} {pytest_cmd} {path})
71 set_tests_properties({name} PROPERTIES {properties})
72 """
73 
74 
76  args = session.ctest_args
77  output = args.get("output_file")
78  if not output:
79  # nothing to do if no output file is specified
80  return
81 
82  properties = 'LABELS "{}" '.format(";".join(args["label"]))
83  properties += " ".join(args["properties"])
84 
85  for path in sorted(session.ctest_files):
86  name = (
87  args["prefix"] + os.path.relpath(path, args["pytest_root_dir"])
88  ).replace("/", ".")
89  if name.endswith(".py"):
90  name = name[:-3]
91  output.write(
92  TEST_DESC_TEMPLATE.format(
93  name=name,
94  path=path,
95  pytest_cmd=args["pytest_command"],
96  properties=properties,
97  )
98  )
99 
100  output.close()
MyGaudiAlg.Path
Path
Definition: MyGaudiAlg.py:26
collect_for_ctest.pytest_collection_finish
def pytest_collection_finish(session)
Definition: collect_for_ctest.py:75
collect_for_ctest.pytest_collection_modifyitems
def pytest_collection_modifyitems(session, config, items)
Definition: collect_for_ctest.py:57
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
collect_for_ctest.pytest_addoption
def pytest_addoption(parser, pluginmanager)
Definition: collect_for_ctest.py:22