Commit 2e3998fa authored by Ezio Melotti's avatar Ezio Melotti

#11468: improve unittest basic example. Initial patch by Florian Preinstorfer.

parent 19276f18
...@@ -80,54 +80,45 @@ The :mod:`unittest` module provides a rich set of tools for constructing and ...@@ -80,54 +80,45 @@ The :mod:`unittest` module provides a rich set of tools for constructing and
running tests. This section demonstrates that a small subset of the tools running tests. This section demonstrates that a small subset of the tools
suffice to meet the needs of most users. suffice to meet the needs of most users.
Here is a short script to test three functions from the :mod:`random` module:: Here is a short script to test three string methods::
import random
import unittest import unittest
class TestSequenceFunctions(unittest.TestCase): class TestStringMethods(unittest.TestCase):
def setUp(self): def test_upper(self):
self.seq = list(range(10)) self.assertEqual('foo'.upper(), 'FOO')
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, list(range(10)))
# should raise an exception for an immutable sequence def test_isupper(self):
self.assertRaises(TypeError, random.shuffle, (1,2,3)) self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_choice(self): def test_split(self):
element = random.choice(self.seq) s = 'hello world'
self.assertTrue(element in self.seq) self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
def test_sample(self): with self.assertRaises(TypeError):
with self.assertRaises(ValueError): s.split(2)
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
A testcase is created by subclassing :class:`unittest.TestCase`. The three A testcase is created by subclassing :class:`unittest.TestCase`. The three
individual tests are defined with methods whose names start with the letters individual tests are defined with methods whose names start with the letters
``test``. This naming convention informs the test runner about which methods ``test``. This naming convention informs the test runner about which methods
represent tests. represent tests.
The crux of each test is a call to :meth:`~TestCase.assertEqual` to check for an 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 expected result; :meth:`~TestCase.assertTrue` or :meth:`~TestCase.assertFalse`
:meth:`~TestCase.assertRaises` to verify that an expected exception gets raised. to verify a condition; or :meth:`~TestCase.assertRaises` to verify that a
These methods are used instead of the :keyword:`assert` statement so the test specific exception gets raised. These methods are used instead of the
runner can accumulate all test results and produce a report. :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 The :meth:`~TestCase.setUp` and :meth:`~TestCase.tearDown` methods allow you
method prior to each test. Likewise, if a :meth:`~TestCase.tearDown` method is to define instructions that will be executed before and after each test method.
defined, the test runner will invoke that method after each test. In the They are covered in more details in the section :ref:`organizing-tests`.
example, :meth:`~TestCase.setUp` was used to create a fresh sequence for each
test.
The final block shows a simple way to run the tests. :func:`unittest.main` 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 provides a command-line interface to the test script. When run from the command
...@@ -142,12 +133,12 @@ line, the above script produces an output that looks like this:: ...@@ -142,12 +133,12 @@ line, the above script produces an output that looks like this::
Passing the ``-v`` option to your test script will instruct :func:`unittest.main` Passing the ``-v`` option to your test script will instruct :func:`unittest.main`
to enable a higher level of verbosity, and produce the following output:: to enable a higher level of verbosity, and produce the following output::
test_choice (__main__.TestSequenceFunctions) ... ok test_isupper (__main__.TestStringMethods) ... ok
test_sample (__main__.TestSequenceFunctions) ... ok test_split (__main__.TestStringMethods) ... ok
test_shuffle (__main__.TestSequenceFunctions) ... ok test_upper (__main__.TestStringMethods) ... ok
---------------------------------------------------------------------- ----------------------------------------------------------------------
Ran 3 tests in 0.110s Ran 3 tests in 0.001s
OK OK
......
...@@ -1085,6 +1085,7 @@ Claudiu Popa ...@@ -1085,6 +1085,7 @@ Claudiu Popa
John Popplewell John Popplewell
Davin Potts Davin Potts
Guillaume Pratte Guillaume Pratte
Florian Preinstorfer
Amrit Prem Amrit Prem
Paul Prescod Paul Prescod
Donovan Preston Donovan Preston
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment