[Python-checkins] cpython (2.7): #11468: improve unittest basic example. Initial patch by Florian Preinstorfer.

ezio.melotti python-checkins at python.org
Tue Mar 24 11:52:48 CET 2015


https://hg.python.org/cpython/rev/4a2a5fddbab3
changeset:   95150:4a2a5fddbab3
branch:      2.7
parent:      95141:4dfe0634d11a
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Tue Mar 24 12:42:41 2015 +0200
summary:
  #11468: improve unittest basic example.  Initial patch by Florian Preinstorfer.

files:
  Doc/library/unittest.rst |  83 ++++++++++++---------------
  Misc/ACKS                |   1 +
  2 files changed, 38 insertions(+), 46 deletions(-)


diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -109,37 +109,29 @@
 running tests.  This section demonstrates that a small subset of the tools
 suffice to meet the needs of most users.
 
-Here is a short script to test three functions from the :mod:`random` module::
-
-   import random
-   import unittest
-
-   class TestSequenceFunctions(unittest.TestCase):
-
-       def setUp(self):
-           self.seq = range(10)
-
-       def test_shuffle(self):
-           # make sure the shuffled sequence does not lose any elements
-           random.shuffle(self.seq)
-           self.seq.sort()
-           self.assertEqual(self.seq, range(10))
-
-           # should raise an exception for an immutable sequence
-           self.assertRaises(TypeError, random.shuffle, (1,2,3))
-
-       def test_choice(self):
-           element = random.choice(self.seq)
-           self.assertTrue(element in self.seq)
-
-       def test_sample(self):
-           with self.assertRaises(ValueError):
-               random.sample(self.seq, 20)
-           for element in random.sample(self.seq, 5):
-               self.assertTrue(element in self.seq)
-
-   if __name__ == '__main__':
-       unittest.main()
+Here is a short script to test three string methods::
+
+  import unittest
+
+  class TestStringMethods(unittest.TestCase):
+
+    def test_upper(self):
+        self.assertEqual('foo'.upper(), 'FOO')
+
+    def test_isupper(self):
+        self.assertTrue('FOO'.isupper())
+        self.assertFalse('Foo'.isupper())
+
+    def test_split(self):
+        s = 'hello world'
+        self.assertEqual(s.split(), ['hello', 'world'])
+        # check that s.split fails when the separator is not a string
+        with self.assertRaises(TypeError):
+            s.split(2)
+
+  if __name__ == '__main__':
+      unittest.main()
+
 
 A testcase is created by subclassing :class:`unittest.TestCase`.  The three
 individual tests are defined with methods whose names start with the letters
@@ -147,16 +139,15 @@
 represent tests.
 
 The crux of each test is a call to :meth:`~TestCase.assertEqual` to check for an
-expected result; :meth:`~TestCase.assertTrue` to verify a condition; or
-:meth:`~TestCase.assertRaises` to verify that an expected exception gets raised.
-These methods are used instead of the :keyword:`assert` statement so the test
-runner can accumulate all test results and produce a report.
-
-When a :meth:`~TestCase.setUp` method is defined, the test runner will run that
-method prior to each test.  Likewise, if a :meth:`~TestCase.tearDown` method is
-defined, the test runner will invoke that method after each test.  In the
-example, :meth:`~TestCase.setUp` was used to create a fresh sequence for each
-test.
+expected result; :meth:`~TestCase.assertTrue` or :meth:`~TestCase.assertFalse`
+to verify a condition; or :meth:`~TestCase.assertRaises` to verify that a
+specific exception gets raised.  These methods are used instead of the
+:keyword:`assert` statement so the test runner can accumulate all test results
+and produce a report.
+
+The :meth:`~TestCase.setUp` and :meth:`~TestCase.tearDown` methods allow you
+to define instructions that will be executed before and after each test method.
+They are covered in more details in the section :ref:`organizing-tests`.
 
 The final block shows a simple way to run the tests. :func:`unittest.main`
 provides a command-line interface to the test script.  When run from the command
@@ -172,18 +163,18 @@
 finer level of control, less terse output, and no requirement to be run from the
 command line.  For example, the last two lines may be replaced with::
 
-   suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
+   suite = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods)
    unittest.TextTestRunner(verbosity=2).run(suite)
 
 Running the revised script from the interpreter or another script produces the
 following output::
 
-   test_choice (__main__.TestSequenceFunctions) ... ok
-   test_sample (__main__.TestSequenceFunctions) ... ok
-   test_shuffle (__main__.TestSequenceFunctions) ... ok
+   test_isupper (__main__.TestStringMethods) ... ok
+   test_split (__main__.TestStringMethods) ... ok
+   test_upper (__main__.TestStringMethods) ... ok
 
    ----------------------------------------------------------------------
-   Ran 3 tests in 0.110s
+   Ran 3 tests in 0.001s
 
    OK
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1077,6 +1077,7 @@
 John Popplewell
 Davin Potts
 Guillaume Pratte
+Florian Preinstorfer
 Amrit Prem
 Paul Prescod
 Donovan Preston

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list