From the course: Programming Foundations: Beyond the Fundamentals
Creating a test case - Python Tutorial
From the course: Programming Foundations: Beyond the Fundamentals
Creating a test case
- I have an important meeting in the morning, so I need to set my alarm clock. Before I go to sleep, I want to make sure the alarm clock is going to wake me up, so I'll temporarily change the alarm to right now. Now, I heard a click, but nothing else happens and that is not going to wake me up. So I check a few things. Well, the alarm is turned on, the hands are lined up. Oh, there's no battery, so let me rectify that. (alarm beeping) And now the alarm works. So now, I know my alarm clock will do what I need and I can set the alarm and go to sleep. When I've written a program that performs as I expect and my interpreter doesn't report any bugs, it can still be helpful to do a little more testing. Especially for more complex code, it can take multiple tests planned out in a thoughtful way to ensure that I'm actually testing all of the possible use cases and every line of my code in all of the possible combinations. To do this kind of testing, programmers create test cases, which are commands or scripts that use the program in a way designed to test a specific scenario. In the forecast.py file, I have a function that takes a temperature in Celsius as an argument, and then uses a set of if and elif statements to decide what advice to print. To fully test this program, i want to execute it with a statement that evaluates to true for the if clause and another statement that's true for the first elif and a third that's true for the second elif. That way, I can verify that values in those three different ranges each produce the results I expect. When you're working on a larger program, it's common to use an entirely separate application for testing your program, but we can create basic test cases within our Python file just by calling this function with specific values. So I'll write some statements to call check_temp, first with a value of 10, and I'll call it again with a value of 30. And finally, call it one more time with a value of 37. These values land within the ranges of each of the three conditions. When I run my code, I expect each of the print statements to be printed in order, so I'll save and then I'll run, and the results are not what I expect. My first test case results in the third print statement being printed. But obviously, I don't want to leave a jacket at home for a temperature below 15, but reviewing the first condition, I notice I coded it wrong. It's supposed to be for cooler conditions, but it's checking whether the value is above 15. So I'll change that sign. It needs to be a less than, and then I'll save it. I'll clear my console and I'll run again. And now, I see Wear a jacket printed first, followed by Pack a jacket. But the third statement, Leave the jacket at home, is never printed. I need to examine that condition again, and when I do, I notice I once again got the sign backward. I'm checking for a temperature below 35, but I really mean to check for above 35. So I'll swap that sign. Instead of less than, I want greater than. Then I'm going to save that. I'm going to clear the console and I'm going to run my code again. And now, I get all three statements printed in order which is what I expect. Test cases can be challenging to write, and personally, when I have an exciting new feature that I want to create, building test cases can feel like a much less interesting task. But test cases are a really useful tool to ensure consistent results from your app and the best possible user experience.