LSST Applications  21.0.0-142-gef555c1e+42c9bccae2,22.0.0+052faf71bd,22.0.0+1c4650f311,22.0.0+40ce427c77,22.0.0+5b6c068b1a,22.0.0+7589c3a021,22.0.0+81ed51be6d,22.0.1-1-g7d6de66+6cae67f2c6,22.0.1-1-g87000a6+314cd8b7ea,22.0.1-1-g8760c09+052faf71bd,22.0.1-1-g8e32f31+5b6c068b1a,22.0.1-10-g779eefa+a163f08322,22.0.1-12-g3bd7ecb+bbeacc25a9,22.0.1-15-g63cc0c1+2a7037787d,22.0.1-17-ge5a99e88+3d2c1afe2e,22.0.1-19-g88addfe+6cae67f2c6,22.0.1-2-g1cb3e5b+84de06d286,22.0.1-2-g8ef0a89+6cae67f2c6,22.0.1-2-g92698f7+1c4650f311,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gb66926d+5b6c068b1a,22.0.1-2-gcb770ba+0723a13595,22.0.1-2-ge470956+ff9f1dc8d5,22.0.1-22-g608e23ac+2ac85e833c,22.0.1-29-g184b6e44e+8b185d4e2d,22.0.1-3-g59f966b+11ba4df19d,22.0.1-3-g8c1d971+f90df4c6d0,22.0.1-3-g997b569+d69a7aa2f8,22.0.1-3-gaaec9c0+4d194bf81c,22.0.1-4-g1930a60+283d9d2f1a,22.0.1-4-g5b7b756+c1283a92b8,22.0.1-4-g8623105+6cae67f2c6,22.0.1-7-gba73697+283d9d2f1a,22.0.1-8-g47d23f5+43acea82f3,master-g5f2689bdc5+40ce427c77,w.2021.38
LSST Data Management Base Package
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
lsst.utils.tests.ExecutablesTestCase Class Reference
Inheritance diagram for lsst.utils.tests.ExecutablesTestCase:

Public Member Functions

def setUpClass (cls)
 
def testSanity (self)
 
def assertExecutable (self, executable, root_dir=None, args=None, msg=None)
 
def create_executable_tests (cls, ref_file, executables=None)
 

Public Attributes

 TESTS_DISCOVERED
 

Static Public Attributes

int TESTS_DISCOVERED = -1
 

Detailed Description

Test that executables can be run and return good status.

The test methods are dynamically created. Callers
must subclass this class in their own test file and invoke
the create_executable_tests() class method to register the tests.

Definition at line 148 of file tests.py.

Member Function Documentation

◆ assertExecutable()

def lsst.utils.tests.ExecutablesTestCase.assertExecutable (   self,
  executable,
  root_dir = None,
  args = None,
  msg = None 
)
Check an executable runs and returns good status.

Prints output to standard out. On bad exit status the test
fails. If the executable can not be located the test is skipped.

Parameters
----------
executable : `str`
    Path to an executable. ``root_dir`` is not used if this is an
    absolute path.
root_dir : `str`, optional
    Directory containing executable. Ignored if `None`.
args : `list` or `tuple`, optional
    Arguments to be provided to the executable.
msg : `str`, optional
    Message to use when the test fails. Can be `None` for default
    message.

Raises
------
AssertionError
    The executable did not return 0 exit status.

Definition at line 171 of file tests.py.

171  def assertExecutable(self, executable, root_dir=None, args=None, msg=None):
172  """Check an executable runs and returns good status.
173 
174  Prints output to standard out. On bad exit status the test
175  fails. If the executable can not be located the test is skipped.
176 
177  Parameters
178  ----------
179  executable : `str`
180  Path to an executable. ``root_dir`` is not used if this is an
181  absolute path.
182  root_dir : `str`, optional
183  Directory containing executable. Ignored if `None`.
184  args : `list` or `tuple`, optional
185  Arguments to be provided to the executable.
186  msg : `str`, optional
187  Message to use when the test fails. Can be `None` for default
188  message.
189 
190  Raises
191  ------
192  AssertionError
193  The executable did not return 0 exit status.
194  """
195 
196  if root_dir is not None and not os.path.isabs(executable):
197  executable = os.path.join(root_dir, executable)
198 
199  # Form the argument list for subprocess
200  sp_args = [executable]
201  argstr = "no arguments"
202  if args is not None:
203  sp_args.extend(args)
204  argstr = 'arguments "' + " ".join(args) + '"'
205 
206  print("Running executable '{}' with {}...".format(executable, argstr))
207  if not os.path.exists(executable):
208  self.skipTest("Executable {} is unexpectedly missing".format(executable))
209  failmsg = None
210  try:
211  output = subprocess.check_output(sp_args)
212  except subprocess.CalledProcessError as e:
213  output = e.output
214  failmsg = "Bad exit status from '{}': {}".format(executable, e.returncode)
215  print(output.decode('utf-8'))
216  if failmsg:
217  if msg is None:
218  msg = failmsg
219  self.fail(msg)
220 
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174

◆ create_executable_tests()

def lsst.utils.tests.ExecutablesTestCase.create_executable_tests (   cls,
  ref_file,
  executables = None 
)
Discover executables to test and create corresponding test methods.

Scans the directory containing the supplied reference file
(usually ``__file__`` supplied from the test class) to look for
executables. If executables are found a test method is created
for each one. That test method will run the executable and
check the returned value.

Executable scripts with a ``.py`` extension and shared libraries
are ignored by the scanner.

This class method must be called before test discovery.

Parameters
----------
ref_file : `str`
    Path to a file within the directory to be searched.
    If the files are in the same location as the test file, then
    ``__file__`` can be used.
executables : `list` or `tuple`, optional
    Sequence of executables that can override the automated
    detection. If an executable mentioned here is not found, a
    skipped test will be created for it, rather than a failed
    test.

Examples
--------
>>> cls.create_executable_tests(__file__)

Definition at line 254 of file tests.py.

254  def create_executable_tests(cls, ref_file, executables=None):
255  """Discover executables to test and create corresponding test methods.
256 
257  Scans the directory containing the supplied reference file
258  (usually ``__file__`` supplied from the test class) to look for
259  executables. If executables are found a test method is created
260  for each one. That test method will run the executable and
261  check the returned value.
262 
263  Executable scripts with a ``.py`` extension and shared libraries
264  are ignored by the scanner.
265 
266  This class method must be called before test discovery.
267 
268  Parameters
269  ----------
270  ref_file : `str`
271  Path to a file within the directory to be searched.
272  If the files are in the same location as the test file, then
273  ``__file__`` can be used.
274  executables : `list` or `tuple`, optional
275  Sequence of executables that can override the automated
276  detection. If an executable mentioned here is not found, a
277  skipped test will be created for it, rather than a failed
278  test.
279 
280  Examples
281  --------
282  >>> cls.create_executable_tests(__file__)
283  """
284 
285  # Get the search directory from the reference file
286  ref_dir = os.path.abspath(os.path.dirname(ref_file))
287 
288  if executables is None:
289  # Look for executables to test by walking the tree
290  executables = []
291  for root, dirs, files in os.walk(ref_dir):
292  for f in files:
293  # Skip Python files. Shared libraries are executable.
294  if not f.endswith(".py") and not f.endswith(".so"):
295  full_path = os.path.join(root, f)
296  if os.access(full_path, os.X_OK):
297  executables.append(full_path)
298 
299  # Store the number of tests found for later assessment.
300  # Do not raise an exception if we have no executables as this would
301  # cause the testing to abort before the test runner could properly
302  # integrate it into the failure report.
303  cls.TESTS_DISCOVERED = len(executables)
304 
305  # Create the test functions and attach them to the class
306  for e in executables:
307  cls._build_test_method(e, ref_dir)
308 
309 
310 @contextlib.contextmanager

◆ setUpClass()

def lsst.utils.tests.ExecutablesTestCase.setUpClass (   cls)
Abort testing if automated test creation was enabled and
no tests were found.

Definition at line 158 of file tests.py.

158  def setUpClass(cls):
159  """Abort testing if automated test creation was enabled and
160  no tests were found."""
161 
162  if cls.TESTS_DISCOVERED == 0:
163  raise RuntimeError("No executables discovered.")
164 

◆ testSanity()

def lsst.utils.tests.ExecutablesTestCase.testSanity (   self)
This test exists to ensure that there is at least one test to be
executed. This allows the test runner to trigger the class set up
machinery to test whether there are some executables to test.

Definition at line 165 of file tests.py.

165  def testSanity(self):
166  """This test exists to ensure that there is at least one test to be
167  executed. This allows the test runner to trigger the class set up
168  machinery to test whether there are some executables to test."""
169  pass
170 

Member Data Documentation

◆ TESTS_DISCOVERED [1/2]

int lsst.utils.tests.ExecutablesTestCase.TESTS_DISCOVERED = -1
static

Definition at line 155 of file tests.py.

◆ TESTS_DISCOVERED [2/2]

lsst.utils.tests.ExecutablesTestCase.TESTS_DISCOVERED

Definition at line 162 of file tests.py.


The documentation for this class was generated from the following file: