LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
Public Member Functions | 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)
 

Static Public Attributes

 TESTS_DISCOVERED
 

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 155 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 178 of file tests.py.

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

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

165  def setUpClass(cls):
166  """Abort testing if automated test creation was enabled and
167  no tests were found."""
168 
169  if cls.TESTS_DISCOVERED == 0:
170  raise Exception("No executables discovered.")
171 

◆ 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 172 of file tests.py.

172  def testSanity(self):
173  """This test exists to ensure that there is at least one test to be
174  executed. This allows the test runner to trigger the class set up
175  machinery to test whether there are some executables to test."""
176  pass
177 

Member Data Documentation

◆ TESTS_DISCOVERED

lsst.utils.tests.ExecutablesTestCase.TESTS_DISCOVERED
static

Definition at line 162 of file tests.py.


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