diff options
| author | Ned Batchelder <ned@nedbatchelder.com> | 2016-01-04 07:18:08 -0500 | 
|---|---|---|
| committer | Ned Batchelder <ned@nedbatchelder.com> | 2016-01-04 07:18:08 -0500 | 
| commit | 78177f0bcdba89b74292405220463e9cb65d4f7a (patch) | |
| tree | aabe20c5d7d066f54f772023a6f1f36c46c2319e /tests/test_testing.py | |
| parent | 1aa9abd82ecde6d5181a17082f666baca00198ef (diff) | |
| download | python-coveragepy-git-78177f0bcdba89b74292405220463e9cb65d4f7a.tar.gz | |
Add a delayed_assertions context manager
--HG--
branch : ast-branch
Diffstat (limited to 'tests/test_testing.py')
| -rw-r--r-- | tests/test_testing.py | 43 | 
1 files changed, 42 insertions, 1 deletions
diff --git a/tests/test_testing.py b/tests/test_testing.py index 9fc7f11d..1dafdd0d 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -6,13 +6,15 @@  import datetime  import os +import re  import sys +import textwrap  import coverage  from coverage.backunittest import TestCase  from coverage.backward import to_bytes  from coverage.files import actual_path -from coverage.test_helpers import EnvironmentAwareMixin, TempDirMixin +from coverage.test_helpers import EnvironmentAwareMixin, TempDirMixin, DelayedAssertionMixin  from tests.coveragetest import CoverageTest @@ -97,6 +99,45 @@ class EnvironmentAwareMixinTest(EnvironmentAwareMixin, TestCase):          self.assertNotIn("XYZZY_PLUGH", os.environ) +class DelayedAssertionMixinTest(DelayedAssertionMixin, TestCase): +    """Test the `delayed_assertions` method.""" + +    def test_delayed_assertions(self): +        # Two assertions can be shown at once: +        msg = re.escape(textwrap.dedent("""\ +            2 failed assertions: +            'x' != 'y' +            - x +            + y + +            'w' != 'z' +            - w +            + z +            """)) +        with self.assertRaisesRegex(AssertionError, msg): +            with self.delayed_assertions(): +                self.assertEqual("x", "y") +                self.assertEqual("w", "z") + +        # It's also OK if only one fails: +        msg = re.escape(textwrap.dedent("""\ +            'w' != 'z' +            - w +            + z +            """)) +        with self.assertRaisesRegex(AssertionError, msg): +            with self.delayed_assertions(): +                self.assertEqual("x", "x") +                self.assertEqual("w", "z") + +        # If an error happens, it gets reported immediately, no special +        # handling: +        with self.assertRaises(ZeroDivisionError): +            with self.delayed_assertions(): +                self.assertEqual("x", "y") +                self.assertEqual("w", 1/0) + +  class CoverageTestTest(CoverageTest):      """Test the methods in `CoverageTest`."""  | 
