Completing Your Stack Class & Measuring Test Coverage
In this lesson you’ll complete your
Stack class by implementing the
pop method, which includes writing tests for it.
So we’ll push a string onto the
stack. Now, this already works because we did this—we did
.push() in a previous test, so we know that
.push() works. The next thing we want to do is pop—we want to pull something off the stack. So if I were to say—actually, maybe let’s push one more item. So I’m going to say
Okay. Now of course, you could stop right here, but you may be thinking to yourself, “Okay, but what happens if I do it again?” Well, what happens in a list is if you keep pulling items from a list, it returns something called an
IndexError. So we have to decide here—and this is just from the programmer’s perspective—when I do
.pop() and I’m at the end of the list, what should happen? Now, you can either do one of two things, generally speaking. You can return nothing—in this case, Python has a datatype called
None, so you could return that—or you could throw an exception saying that you’re doing something illegal. In this case here, what I want to do is just return
None, but yes, of course you could just throw an exception.
Okay? So this is our test. I put two things onto the stack and then I popped them off in reverse order.
"world" comes off first,
"hello" comes off second. The stack is then empty, and then if I do a
.pop() on an empty stack, then I want it to return
So if I come up here and I type in
def pop(self)—now, does this
.pop() method need any additional parameters? And as you can see, by looking at this—this signature—that it doesn’t need any parameters. It’s empty.
Lists, of course, have an internal
.pop() method, so we can actually use that for our application. Okay. So let’s try this. There’s a slight problem though, in that when you pop off of an empty list, it actually does throw an error message, so these first two may pass, but this bottom one will fail.
But if you happen to have an empty list and you pop from that, then you’re getting an
IndexError. Okay, so we’re either going to get something good or
None. And then at the end, I’m just going to return
05:54 So that looks really good. We have our fixture, and so—yeah! This data structure is effectively complete. Once again, it’s a simplified version. You could add additional features to it, but the basic features any stack needs, of course, is push and pop, and we did implement that, so this is great. The last thing I want to do—and I mentioned this—is test coverage.
--cov. And what that’s going to do is it’s going to rerun your tests of course, but it’s going to show you all your files—your
stack, and your test—and it’s going to show you the number of statements that you have in each one.
07:11 So not only A, did our test pass, but we have 100% test coverage. Okay. So, this concludes this video. Hopefully it was useful. Once again, this video is how to implement code or write an application using test driven development, where you actually write your tests first and the implementation second. Hopefully, this motivates you a little bit to try this in your next application. Thank you.
Become a Member to join the conversation.