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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Test Move Method

00:00 So in the previous lesson, I kind of finished writing this big .move() method. So now it’s time to test it out. I will run the code and then I will have to make some animals.

00:15 An instance of a Pig

00:21 and an instance of a Dog, and

00:31 an instance of a Barn that holds only one animal. Also an instance of a Field that holds ten animals. Okay, I have a Pig, a Dog, a Barn, and a Field all initialized, and now I should be able to move around and test out the new .move() function.

00:54 So the pig._location currently is None. If I print it, it’s going to tell us that it’s None

01:03 because we just initialized the pig, and it just lives in the void for now.

01:09 Now I’m going to move the pig. The pig is going to go to the barn.

01:17 I get as an output, Lizzy moved to the barn. Sounds good. Let’s see whether it actually happened. pig. In this call, ._location should point to the barn object. Great.

01:28 And from the other direction, if I say barn.animals, yes, I have a list that contains now one Pig object. That worked. What if I try to move the dog also into the barn? That shouldn’t work.

01:43 The barn in this case only has one space. So if I try to do dog.move() into the barn, then it tells me The barn is full. Let’s see if anything happened.

01:56 barn.animals still only contains the Pig, so that’s good. And dog._location is still None. So that’s great. Now, can I move the dog to the field?

02:11 That should work. That’s the same what we did earlier with the pig. Puppy moved to the field. dog._location points to the Field object. That’s great.

02:21 And the field object contains a Dog instance now. And now I want to move the sheep. No, the pig. I don’t have a sheep here. .move() to the field as well.

02:36 That should take it out of the barn and move it into the field. So the output says Lizzy moved to the field. That’s great. Now let’s look at pig._location.

02:49 It points to the Field object. That’s great. Now let’s see if the field has also been updated. field.animals contains two items, A Dog and the Pig instance.

03:00 Great, that’s a bit of testing, just a manual testing that I did right now. But it seems like the .move() method works like I expected it to.

03:11 Guess what? There is a bug in my .move() method after all. It’s a subtle logical bug that managed to escape my crude manual testing as you’ve just seen.

03:21 I only noticed the bug later on. That’s a good reminder that manual testing isn’t very reliable and that writing tests for your code that you can run repeatedly is a much better way to go.

03:32 Anyways, this bug won’t affect the functionality of my farm much, which is why you’ll continue to see this buggy version of .move() stick around in the upcoming lessons.

03:42 But that doesn’t mean you can’t go off on a side quest. Investigate my buggy .move() method, and propose a fix in the comments. I’ll redeem myself with a bugfix lesson just before the end of this course.

03:59 The interesting thing that you’re doing here is that you’re using some sort of an element of composition, I guess. This is called aggregation, because you’re not technically composing an object from another object. Well, I guess Animal has some sort of composition in there because we have a ._location that is going to be the Location object, and here we have in the FarmLocation attribute .animals, we see a form of aggregation where you’re adding Animal objects to an internal list of the FarmLocation object or its children.

04:35 Great. I’m going to clean up the comments here just to make this a bit cleaner. Do you want to print a message when I remove it? Not really. I’m just going to inform when it enters the location.

04:49 I think that should be enough. And that’s the .move() method. I think at this point we’ve done three methods, so I will remove this note as well. Good job.

05:02 Let’s take a look at the current implementation of your farm in the next lesson.

Become a Member to join the conversation.