← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~kampka/testtools/assert-is-none into lp:testtools

 

Christian Kampka has proposed merging lp:~kampka/testtools/assert-is-none into lp:testtools.

Requested reviews:
  Jonathan Lange (jml)

For more details, see:
https://code.launchpad.net/~kampka/testtools/assert-is-none/+merge/64868

It would be nice if TestCase had some convenience methods to allow testing for 'None' values, similar to pythons own unittest2 TestCase.
-- 
https://code.launchpad.net/~kampka/testtools/assert-is-none/+merge/64868
Your team testtools developers is subscribed to branch lp:testtools.
=== modified file 'testtools/testcase.py'
--- testtools/testcase.py	2011-05-27 17:00:32 +0000
+++ testtools/testcase.py	2011-06-16 17:21:19 +0000
@@ -29,6 +29,7 @@
 from testtools.matchers import (
     Annotate,
     Equals,
+    Not,
     )
 from testtools.monkey import patch
 from testtools.runtest import RunTest
@@ -316,6 +317,28 @@
         if needle not in haystack:
             self.fail('%r not in %r' % (needle, haystack))
 
+    def assertIsNone(self, observed, message=''):
+        """Assert that 'observed' is equal to None.
+
+        :param observed: The observed value.
+        :param message: An optional message describing the error.
+        """
+        matcher = Equals(None)
+        if message:
+            matcher = Annotate(message, matcher)
+        self.assertThat(observed, matcher)
+
+    def assertIsNotNone(self, observed, message=''):
+        """Assert that 'observed' is not equal to None.
+
+        :param observed: The observed value.
+        :param message: An optional message describing the error.
+        """
+        matcher = Not(Equals(None))
+        if message:
+            matcher = Annotate(message, matcher)
+        self.assertThat(observed, matcher)
+
     def assertIs(self, expected, observed, message=''):
         """Assert that 'expected' is 'observed'.
 

=== modified file 'testtools/tests/test_testtools.py'
--- testtools/tests/test_testtools.py	2011-05-12 23:01:56 +0000
+++ testtools/tests/test_testtools.py	2011-06-16 17:21:19 +0000
@@ -501,6 +501,29 @@
         self.assertFails(expected_error, self.assertEquals, a, b)
         self.assertFails(expected_error, self.failUnlessEqual, a, b)
 
+    def test_assertIsNone(self):
+        self.assertIsNone(None)
+
+        expected_error = '\n'.join([
+            'Match failed. Matchee: "0"',
+            'Matcher: Equals(None)',
+            'Difference: None != 0',
+            ''
+            ])
+        self.assertFails(expected_error, self.assertIsNone, 0)
+
+    def test_assertIsNotNone(self):
+        self.assertIsNotNone(0)
+        self.assertIsNotNone("0")
+
+        expected_error = '\n'.join([
+            'Match failed. Matchee: "None"',
+            'Matcher: Not(Equals(None))',
+            'Difference: None matches Equals(None)',
+            ''
+            ])
+        self.assertFails(expected_error, self.assertIsNotNone, None)
+        
 
 class TestAddCleanup(TestCase):
     """Tests for TestCase.addCleanup."""


Follow ups