~testing-cabal/ubuntu/natty/subunit/daily-build-packaging

« back to all changes in this revision

Viewing changes to python/subunit/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2011-02-28 09:32:28 UTC
  • mfrom: (135.1.5 python3)
  • Revision ID: jelmer@samba.org-20110228093228-1hyelaw7zkfe91e1
Tags: 0.0.6+bzr140-0ubuntu1
Merge new upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
#  license at the users choice. A copy of both licenses are available in the
7
7
#  project source as Apache-2.0 and BSD. You may not use this file except in
8
8
#  compliance with one of these two licences.
9
 
#  
 
9
#
10
10
#  Unless required by applicable law or agreed to in writing, software
11
11
#  distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
12
12
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
49
49
The test outcome methods ``addSuccess``, ``addError``, ``addExpectedFailure``,
50
50
``addFailure``, ``addSkip`` take an optional keyword parameter ``details``
51
51
which can be used instead of the usual python unittest parameter.
52
 
When used the value of details should be a dict from ``string`` to 
 
52
When used the value of details should be a dict from ``string`` to
53
53
``testtools.content.Content`` objects. This is a draft API being worked on with
54
54
the Python Testing In Python mail list, with the goal of permitting a common
55
55
way to provide additional data beyond a traceback, such as captured data from
58
58
 
59
59
The ``tags(new_tags, gone_tags)`` method is called (if present) to add or
60
60
remove tags in the test run that is currently executing. If called when no
61
 
test is in progress (that is, if called outside of the ``startTest``, 
 
61
test is in progress (that is, if called outside of the ``startTest``,
62
62
``stopTest`` pair), the the tags apply to all sebsequent tests. If called
63
63
when a test is in progress, then the tags only apply to that test.
64
64
 
87
87
Similarly, ``IsolatedTestCase`` is a base class which can be subclassed to get
88
88
tests that will fork() before that individual test is run.
89
89
 
90
 
`ExecTestCase`` is a convenience wrapper for running an external 
 
90
`ExecTestCase`` is a convenience wrapper for running an external
91
91
program to get a Subunit stream and then report that back to an arbitrary
92
92
result object::
93
93
 
98
98
 
99
99
     def test_script_two(self):
100
100
         './bin/script_two'
101
 
 
 
101
 
102
102
 # Normally your normal test loading would take of this automatically,
103
103
 # It is only spelt out in detail here for clarity.
104
104
 suite = unittest.TestSuite([AggregateTests("test_script_one"),
257
257
 
258
258
    def _outcome(self, offset, line, no_details, details_state):
259
259
        """An outcome directive has been read.
260
 
        
 
260
 
261
261
        :param no_details: Callable to call when no details are presented.
262
262
        :param details_state: The state to switch to for details
263
263
            processing of this outcome.
385
385
 
386
386
    def _outcome_label(self):
387
387
        return "failure"
388
 
 
 
388
 
389
389
 
390
390
class _ReadingErrorDetails(_ReadingDetails):
391
391
    """State for the subunit parser when reading error details."""
433
433
 
434
434
class TestProtocolServer(object):
435
435
    """A parser for subunit.
436
 
    
 
436
 
437
437
    :ivar tags: The current tags associated with the protocol stream.
438
438
    """
439
439
 
445
445
            subunit protocol should be written to. This allows custom handling
446
446
            of mixed protocols. By default, sys.stdout will be used for
447
447
            convenience.
448
 
        :param forward_stream: A stream to forward subunit lines to. This 
 
448
        :param forward_stream: A stream to forward subunit lines to. This
449
449
            allows a filter to forward the entire stream while still parsing
450
450
            and acting on it. By default forward_stream is set to
451
451
            DiscardStream() and no forwarding happens.
514
514
 
515
515
    def readFrom(self, pipe):
516
516
        """Blocking convenience API to parse an entire stream.
517
 
        
 
517
 
518
518
        :param pipe: A file-like object supporting readlines().
519
519
        :return: None.
520
520
        """
535
535
 
536
536
class TestProtocolClient(testresult.TestResult):
537
537
    """A TestResult which generates a subunit stream for a test run.
538
 
    
 
538
 
539
539
    # Get a TestSuite or TestCase to run
540
540
    suite = make_suite()
541
541
    # Create a stream (any object with a 'write' method)
558
558
 
559
559
    def addError(self, test, error=None, details=None):
560
560
        """Report an error in test test.
561
 
        
 
561
 
562
562
        Only one of error and details should be provided: conceptually there
563
563
        are two separate methods:
564
564
            addError(self, test, error)
573
573
 
574
574
    def addExpectedFailure(self, test, error=None, details=None):
575
575
        """Report an expected failure in test test.
576
 
        
 
576
 
577
577
        Only one of error and details should be provided: conceptually there
578
578
        are two separate methods:
579
579
            addError(self, test, error)
588
588
 
589
589
    def addFailure(self, test, error=None, details=None):
590
590
        """Report a failure in test test.
591
 
        
 
591
 
592
592
        Only one of error and details should be provided: conceptually there
593
593
        are two separate methods:
594
594
            addFailure(self, test, error)
603
603
 
604
604
    def _addOutcome(self, outcome, test, error=None, details=None):
605
605
        """Report a failure in test test.
606
 
        
 
606
 
607
607
        Only one of error and details should be provided: conceptually there
608
608
        are two separate methods:
609
609
            addOutcome(self, test, error)
721
721
 
722
722
class RemotedTestCase(unittest.TestCase):
723
723
    """A class to represent test cases run in child processes.
724
 
    
 
724
 
725
725
    Instances of this class are used to provide the Python test API a TestCase
726
726
    that can be printed to the screen, introspected for metadata and so on.
727
727
    However, as they are a simply a memoisation of a test that was actually
806
806
 
807
807
class IsolatedTestCase(unittest.TestCase):
808
808
    """A TestCase which executes in a forked process.
809
 
    
 
809
 
810
810
    Each test gets its own process, which has a performance overhead but will
811
811
    provide excellent isolation from global state (such as django configs,
812
812
    zope utilities and so on).
819
819
 
820
820
class IsolatedTestSuite(unittest.TestSuite):
821
821
    """A TestSuite which runs its tests in a forked process.
822
 
    
 
822
 
823
823
    This decorator that will fork() before running the tests and report the
824
824
    results from the child process using a Subunit stream.  This is useful for
825
825
    handling tests that mutate global state, or are testing C extensions that
871
871
 
872
872
def TAP2SubUnit(tap, subunit):
873
873
    """Filter a TAP pipe into a subunit pipe.
874
 
    
 
874
 
875
875
    :param tap: A tap pipe/stream/file object.
876
876
    :param subunit: A pipe/stream/file object to write subunit results to.
877
877
    :return: The exit code to exit with.
1028
1028
    that has been encoded into the stream. The ``unittest.TestCase`` ``debug``
1029
1029
    and ``countTestCases`` methods are not supported because there isn't a
1030
1030
    sensible mapping for those methods.
1031
 
    
 
1031
 
1032
1032
    # Get a stream (any object with a readline() method), in this case the
1033
1033
    # stream output by the example from ``subunit.TestProtocolClient``.
1034
1034
    stream = file('tests.log', 'rb')
1035
 
    # Create a parser which will read from the stream and emit 
 
1035
    # Create a parser which will read from the stream and emit
1036
1036
    # activity to a unittest.TestResult when run() is called.
1037
1037
    suite = subunit.ProtocolTestCase(stream)
1038
1038
    # Create a result object to accept the contents of that stream.
1076
1076
 
1077
1077
class TestResultStats(testresult.TestResult):
1078
1078
    """A pyunit TestResult interface implementation for making statistics.
1079
 
    
 
1079
 
1080
1080
    :ivar total_tests: The total tests seen.
1081
1081
    :ivar passed_tests: The tests that passed.
1082
1082
    :ivar failed_tests: The tests that failed.
1127
1127
 
1128
1128
def get_default_formatter():
1129
1129
    """Obtain the default formatter to write to.
1130
 
    
 
1130
 
1131
1131
    :return: A file-like object.
1132
1132
    """
1133
1133
    formatter = os.getenv("SUBUNIT_FORMATTER")