doctest Module and assert Statements
In this lesson, you’ll learn about the
doctest module and
doctest module is very useful to test code in a quick and easy manner by specifying an input and the correct output in the docstring and running
python3 -m doctest file.py. You can check out the Python documentation on the
assert statements are useful to write tests and check conditions in real time. The syntax looks like this:
assert <cond>, ([error_msg])
The error message is optional. Here’s an example:
>>> x = 5 >>> assert x > 0, "x is not positive" >>> x = -1 >>> assert x > 0, "x is not positive" AssertionError: x is not positive
To learn more about
assert statements and Python exceptions, check out Assertions and Try/Except.
00:00 In this video, you’ll learn how to test your code in an interview. Obviously, you could use print statements—that’s probably your go-to to make sure that the output is correct. But here are some better ways.
Let’s have a
f() function that just takes in no arguments, just
self. Put our docstring, and here you’d probably write the function description, the argument types, maybe, the return type, anything else here.
We want it to print out
Hello world, and it should return the string
'Hello world'. Remember,
print() removes the quotes in the REPL, and when you return a string, it will have the quotes. So, our code would look something like
return 'Hello World'.
Again, you can do this with a function.
def f() takes in an
x. Maybe we want to validate input now. So, if we pass in a positive number, our function will print out
Args: 10 and then just return a string
'Valid Input'. But if you pass in a negative number, it should throw an error. And let’s say you’d want to throw a
ValueError—because that’s a good error to throw when you have wrong arguments—and instead, say
return 'Valid input'. Save, run. We see that
5 passed and 0 failed. So, doctests are really useful if, one, they give you a take-home coding project, where you actually have to write your own classes and write your own docstrings and your own tests, and give it back to them, and then they’ll look at whatever criteria. Or, if there’s a phone interview and they’re actually screen sharing your screen, and so you can actually run the
Or maybe, you’re on an onsite and you’re using their computer and you actually have access to a terminal and a text editor. But what if you’re at a phone interview and the text code-sharing editor doesn’t have
doctest module or a terminal? Well, then it’s really useful to use
The syntax is
assert <condition> and then
<error>. And this
<error> is optional to pass in. But basically, what happens is if the condition is
True, then you keep going with the code. If the condition is
False, then you raise the error. So it’s almost the opposite of this. This, if the condition is
True you raise the error, while this, if the condition is
True you keep going.
and then do
g(10), nothing happens.
g(-1), you get an
AssertionError: Invalid input. This is useful for checking arguments or maybe raising errors, but how would you use asserts to test your code? Let’s write a function
lst_one_more(). Not the best function name, but it’ll basically take in two lists, and let’s write a docstring—not a doctest, but just a docstring—saying
"This will mutate lst1"—which means change it—
"so that at index
`i`, lst1[i] = lst2[i] + 1".
So, even if the interviewer tells you something should be something, you should still always get in the habit of checking arguments. You could use the
assert statement to check the arguments—
len(lst1) == len(lst2), and then just raise some error like
"Length of lists not the same".
for i, x in enumerate(lst1):
assert x == lst2[i] + 1. Now exit this, run our code—nothing happened, which is good. That means that this
assert statement never raised an error, but let’s remove this
+ 1 here, run our code again, and now you get an
AssertionError with the line number.
This is really useful because in an interview, many of the times, they want you to write your own tests and they want you to run the tests as you’re working on the problem. And as the tests grow, it’s very hard to just eyeball if your output is correct. For example, if I used a print statement—
x, lst2[i]—saved it, print it—I mean, it’s a little easy to eyeball that it’s
1 +, but you can imagine with a bunch of different lines, a bunch of different output—it’s hard to eyeball and every minute counts, so using an
assert will guarantee that you’re doing this check automatically instead of eyeballing it. As always, I will link the documentation for
doctest and the
assert statement down below.
Become a Member to join the conversation.