Chapter 2
■
GettinG Started
22
Navigate to the chapter02 directory and type python3 to invoke Python 3 in
interpreter mode.
pi@raspberrypi:~/book/code/chapter02 $ pwd
/home/pi/book/code/chapter02
pi@raspberrypi:~/book/code/chapter02 $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Import the test module you just created with the following statement:
>>> import test_module01
You can use the help() function to see the docstrings
of the module and its
members, as follows.
>>> help(test_module01)
The output is as follows:
Help on module test_module01:
NAME
test_module01
DESCRIPTION
This is test_module01.
This is example of multiline docstring.
CLASSES
builtins.object
TestClass01
class TestClass01(builtins.object)
| This is TestClass01.
|
| Methods defined here:
|
| test_case01(self)
| This is test_case01().
|
| -------------------------------------------------------------------
Chapter 2
■
GettinG Started
23
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
FUNCTIONS
test_function01()
This is test_function01().
FILE
/home/pi/book/code/chapter02/test_module01.py
You can see the docstring of the individual members using help(). Run the following
statements and see the output for yourself.
>>> help(test_module01.TestClass01)
>>> help(test_module01.TestClass01.test_case01)
>>> help(test_module01.test_function01)
As mentioned earlier, a docstring becomes the __doc__ special
attribute of that
object. You can also use the print() function to see the docstring of a module and its
members. The following interactive Python session demonstrates that.
>>> import test_module01
>>> print(test_module01.__doc__)
This is test_module01.
This is example of multiline docstring.
>>> print(test_module01.TestClass01.__doc__)
This is TestClass01.
>>> print(test_module01.TestClass01.test_case01.__doc__)
This is test_case01().
>>> print(test_module01.test_function01.__doc__)
This is test_function01().
>>>
You can find detailed information about the Python docstring on the following PEP
pages.
https://www.python.org/dev/peps/pep-0256
https://www.python.org/dev/peps/pep-0257
https://www.python.org/dev/peps/pep-0258
Chapter 2
■
GettinG Started
24
In
the next section, you will learn to use docstrings to write simple test cases and
execute them with doctest.
A Brief Introduction to doctest
doctest is the lightweight unit testing framework in Python that
uses docstrings to test
automation. The doctest is packaged with the Python interpreter, so you do not have to
install anything separately to use it. It is part of Python's standard
library and adheres to
Python's “batteries-included” philosophy.
■
Note if you’re interested, you can read python's batteries-included philosophy on the
pep 206 page (
https://www.python.org/dev/peps/pep-0206
).
The code in Listing
2-2
is a simple example of a test module with two functions and
two tests for each function.
Do'stlaringiz bilan baham: