~testing-cabal/testtools/trunk

« back to all changes in this revision

Viewing changes to testtools/testresult/real.py

  • Committer: GitHub
  • Author(s): Free Ekanayaka
  • Date: 2017-04-11 13:13:10 UTC
  • Revision ID: git-v1:01d4a9b59ecb93701838b8df5ee8fbbd3d22b0f4
Add ResourcedToStreamDecorator test result decorator for testresources integration (#243)

This new decorator implements the TestResult protocol extension supported by test resources. For example, tt makes it possible to easily have resource-related events streamed to subunit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
    'ExtendedToOriginalDecorator',
7
7
    'ExtendedToStreamDecorator',
8
8
    'MultiTestResult',
 
9
    'ResourcedToStreamDecorator',
9
10
    'StreamFailFast',
10
11
    'StreamResult',
11
12
    'StreamSummary',
1688
1689
        return super(ExtendedToStreamDecorator, self).wasSuccessful()
1689
1690
 
1690
1691
 
 
1692
class ResourcedToStreamDecorator(ExtendedToStreamDecorator):
 
1693
    """Report ``testresources``-related activity to StreamResult objects.
 
1694
 
 
1695
    Implement the resource lifecycle TestResult protocol extension supported
 
1696
    by the ``testresources.TestResourceManager`` class. At each stage of a
 
1697
    resource's lifecycle, a stream event with relevant details will be
 
1698
    emitted.
 
1699
 
 
1700
    Each stream event will have its test_id field set to the resource manager's
 
1701
    identifier (see ``testresources.TestResourceManager.id()``) plus the method
 
1702
    being executed (either 'make' or 'clean').
 
1703
 
 
1704
    The test_status will be either 'inprogress' or 'success'.
 
1705
 
 
1706
    The runnable flag will be set to False.
 
1707
    """
 
1708
 
 
1709
    def startMakeResource(self, resource):
 
1710
        self._convertResourceLifecycle(resource, 'make', 'start')
 
1711
 
 
1712
    def stopMakeResource(self, resource):
 
1713
        self._convertResourceLifecycle(resource, 'make', 'stop')
 
1714
 
 
1715
    def startCleanResource(self, resource):
 
1716
        self._convertResourceLifecycle(resource, 'clean', 'start')
 
1717
 
 
1718
    def stopCleanResource(self, resource):
 
1719
        self._convertResourceLifecycle(resource, 'clean', 'stop')
 
1720
 
 
1721
    def _convertResourceLifecycle(self, resource, method, phase):
 
1722
        """Convert a resource lifecycle report to a stream event."""
 
1723
 
 
1724
        # If the resource implements the TestResourceManager.id() API, let's
 
1725
        # use it, otherwise fallback to the class name.
 
1726
        if safe_hasattr(resource, "id"):
 
1727
            resource_id = resource.id()
 
1728
        else:
 
1729
            resource_id = "%s.%s" % (
 
1730
                resource.__class__.__module__, resource.__class__.__name__)
 
1731
 
 
1732
        test_id = '%s.%s' % (resource_id, method)
 
1733
 
 
1734
        if phase == 'start':
 
1735
            test_status = 'inprogress'
 
1736
        else:
 
1737
            test_status = 'success'
 
1738
 
 
1739
        self.status(
 
1740
            test_id=test_id, test_status=test_status, runnable=False,
 
1741
            timestamp=self._now())
 
1742
 
 
1743
 
1691
1744
class StreamToExtendedDecorator(StreamResult):
1692
1745
    """Convert StreamResult API calls into ExtendedTestResult calls.
1693
1746