Doctest is a testing framework, it serves well to document functions but when there
are a lot of tests it can obscure the code inside a function.
There were other testing frameworks that don't suffer from this problem, one of
them is unit test which comes with Python. In order to use unit test, we need to
learn a little bit more about some Python language features and, in particular, how
to create your own type. You've used lots of types, strings,
functions, modules, dictionaries, list, Booleans and more.
All of these were written by programmers working for the Python Software
Foundation. The organization that manages the Python
programming language. We'll start by reexamining some of these
existing types, and then we'll see how to create our own.
Module math has a lot of functions. One of them is math.ceil, which returns
the ceiling. Of a number.
That's the smallest integer that is greater than or equal to the argument.
We called it like this. Module math also has data, in particular,
variables e and pi. Importing math creates a variable called
math, that refers to a namespace, that's a chunk of memory with names like a cosine
ceiling, and so on, listed inside. Each of which point to a function object.
As well as e and pi, which refer to floating point numbers.
As you know, types such as int and str. Contain a special kind of function called
a Method. Here is the output of calling function of
help on type str. Str is a class, and a class is Python's
way of defining a type. An example of a method in class str, is
count. String method count, counts how many times
one strings occurs in a another. Because str is a built in pipe, we already
have variable str, it refers to a class, rather than a module, but it looks much
the same, with names like center and count listed inside, each of which refer to
method objects. Much like we write math.seal with argument
3.2 in order to call the sealing function in module math, we write str.count and
pass in a string, and the substring to search or count in that first string.
Again as you've seen because methods are special in that they expect an instance of
the class as the first argument we can, in the object oriented way call syzygy.count
y. This is the standard way.
To call methods. Class string has a lot of helpful methods
but there are some that are missing. We're going to write our own class that
customizes class string, our class will have all the string methods but will also
add one that checks whether a string starts and ends with the same letter.
Here's how we start our class. The first line tells Python that we are
defining a new class called WordplayStr, and that it's based on class str.
We say that wordplayStr is a subclass of str.
It inherits all of the features of str. As we said our class is a string class
that has a few additional features. Lets make a new one, we can find out its
length and we can call string methods, you can even compare it to a regular string.
So far it behaves identically to a string. Its type, however, is not str it's word
play str. Lets create a file where we can write our
new class definition and including the methods that we are going to add.
Here's the class definition as we have typed it in the shell.
Can add methods to our class. Let's add one that returns true, if a word
starts and ends with the same letter and returns false otherwise.
Following the design recipe, the first step is to come up with examples.
We'll create a WordplayStr where the first and last letters are the same.
Then when we call our function the providing, maybe we'll try same start and
bend. We hope that it returns true.
If we create a WordplayStr where the first and last letters are not the same, then
when we call our method, we expect false as the result.
The name, same start and end for our method seems fine so let's fill in the
header. There is one parameter which we call self,
that's the WordplayStr that owns this method.
The type is WordplayStr and this method returns a Boolean.
Description is returned whether self starts and ends with the same letter.
Because class WordplayStr inherits from class str, we have access to all of the
string functionality. That means that we can use indexing, we
can look at index zero. And compare to the, letter that is at
index minus one. And same start and end should produce true
if those two are the same, and it should produce false otherwise.
This won't work on an empty string, so we'll add a precondition to forbid that.
Let's add an if name equals main section at the bottom to try out our doctest.
We run it, our tests pass. It's important to note that we haven't
changed class string. It doesn't know about these new methods.
Only our subclass, WordplayStr has the ability.
To check to see if it has the same start and end character.
We'll see a deeper explanation of classes later in this course, but you are now
ready to tackle Python's unit test module.