Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Make Testing Easier

00:00 In the last lesson, I’ve identified what the issue is, which is that a pig thinks it’s still in the field when it’s actually not part of field.animals anymore, which only occurs when I try to move an animal that already has a location into a location that is full. Kind of subtle.

00:19 We can go ahead and fix that, but to fix that, I want to be able to run my tests a little more often. So I’ll take a small step towards automated testing by just adding the code that I’ve played around with in the interactive shell at the bottom of the script so that it just runs every time that I run the script.

00:41 So down here, I’m going to create the necessary instances.

00:47 I’ll need a field, a barn, a dog, and a pig. I’ll move the dog to the barn, stick a quick note here what’s going on, fill the barn,

01:03 set up the objects,

01:07 and then I need to move the pig to the field. Move the pig, and once it has the field as its ._location, I can try to move it again into the barn.

01:25 And this is when the error occurs because it will then assume that the pig is still in the field, but it’s not going to be part of the field.animals anymore.

01:36 So let’s print the output. print(pig.location). This should be field, which it is, and field.animals should still contain the pig, but it doesn’t.

01:59 So this is rewriting the testing code I wrote over here in the interactive shell. Try it out and see if as the output we get a field is what I expect to get from this first print() call.

02:12 And then from the second print() call, I currently expect to get an empty list, but it should contain the pig. So that’s the bug that we’re going to fix.

02:23 Let me try and run that. So I get field and the empty square brackets as expected. Now, why not? Let’s make it a little more readable and add a message.

02:37 I’m going to say "Pig is in the"

02:43 location and "Field contains" and then the animals. Run it again. Pig is in the field. Field contains nothing, but that’s what we want to fix.

02:59 The pig should still be in there. Okay, so this is not great automated testing at all. All I did was add a bit of code at the bottom of my script, and there’s lots of better ways of doing this, but I want to keep it simple here and just avoid that I have to enter these instances and re-create them every time until I fix this bug. Okay, so next step is to actually fix the bug.

Become a Member to join the conversation.