Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Testing Functions With AssertionError

Resource linked in this lesson: Using Python’s assert to Debug and Test Your Code

00:00 The AssertionError is a unique exception because it’s raised as a consequence of using the assert statement. The assert statement uses the syntax assert expression followed by an optional assertion message string, and it raises an AssertionError if expression evaluates to false.

00:18 What is this good for? Well, it can be used to test or sanity check conditions in your program, helping you catch errors early while in development. assert should strictly be used for development, not production, because assertions can be turned off when you run in optimized mode using the command-line options -O or -OO. Once again, meet me in your IDE of choice to practice testing your code with assertions.

00:48 In this example, you’ll be using assertions to validate the results of a FizzBuzz program. What is FizzBuzz? It’s a popular programming quiz used to test a coder’s skill with writing control flow logic.

01:00 For this example, FizzBuzz should be a function that receives a number and either returns that number or one of three strings: Fizz if the number is divisible by three, Buzz if the number is divisible by five, or FizzBuzz if the number is divisible by both three and five. Start by defining an empty FizzBuzz function with a pass statement in the function body: def fizzbuzz (number): pass and before writing the implementation, you can think of some examples and the results that should be returned.

01:34 if __name__ == "__main__": assert fizzbuzz(9) is equal to Fizz,

01:45 assert fizzbuzz(10) is equal to Buzz,

01:52 assert fizzbuzz(15) is equal to FizzBuzz,

01:58 assert fizzbuzz(7) is equal to 7. And then print “All tests pass.” These are your test cases. If any of these expressions fail, an AssertionError will be raised and you’ll know that FizzBuzz isn’t working correctly.

02:14 Now let’s write an implementation, deleting pass

02:20 if number % 3 == 0: return the string Fizz elif number % 5 == 0: return the string Buzz elif number % 15 == 0: return the string FizzBuzz.

02:40 else: return number as is, and let’s see if it works. Save the file, and open the integrated terminal and run the file. python fizzbuzz.py

02:59 And AssertionError. It looks like this is triggered by the line assert fizzbuzz(15) == "FizzBuzz". So FizzBuzz is not returning FizzBuzz when passed the integer 15.

03:12 Can you maybe see why? Yep. It’s because the FizzBuzz condition is unreachable because anytime a number is divisible by three, the Fizz condition will be triggered first. All you need to do is move the FizzBuzz condition to the first spot.

03:27 Grab these two lines, move them up,

03:33 and change my if to an elif and my elif to an if. Save the file and see if the rearranged FizzBuzz can pass the tests.

03:42 python fizzbuzz.py and all tests pass. Perfect! As you’ve seen, assert can be really useful, and this is just the tip of the iceberg.

03:55 If you want to learn more, check out using Python’s assert to debug and test your code.

04:01 And that was the last exception I had for you. All that we have left to do is review and wrap up. I’ll see you in the summary.

Become a Member to join the conversation.