+ 1

Hi,all what is the purpose of the following statement with pytest.raise(TypeError)?

I have searched a lot but all what I found is testing exception what does it mean?

20th Oct 2024, 10:46 AM
Yusof
Yusof - avatar
5 Answers
+ 5
pytest - is a package that helps you create tests for your app. You can write a bunch of tests for your various functions and then compare the results to see if your function did what it's supposed to do. raises - is a function that checks if your app raised an error. Sometimes you want your code to raise an error in certain circumstances. When you are testing a function to make sure it raises an error, this function does that. TypeError - is the error type. This means the data is the wrong type. There are a number of errors that can be raised or checked for. So in this case, there is no context to what you are asking about. But this one command would be a test to see if the function you are testing raises a TypeError. NOTE: Instead of googling the whole thing - just google the first part and read the docs from there. You're question is about PyTest. Then google that. When you are on the PyTest documentation, look for RAISES.
20th Oct 2024, 12:35 PM
Jerry Hobby
Jerry Hobby - avatar
+ 3
Yusof yeah your right according to your code it is integer based code and while testing you gave the string so it raised the type error so it's handling it properly so that users don't type in wrong values other than integers.
20th Oct 2024, 3:34 PM
Aysha
Aysha - avatar
+ 2
Yusof, When you write a program that might have hundreds of functions, it's easy to have "breaking changes" when you update one of those functions. Imagine having square(n) work a certain way. Then later you modify it and it does something in a slightly different way. That could cause problems when another part of your application uses the square(n) function. Changing existing production code can sometimes be a big problem. Imagine a programming team. One person designs specifications for all the functions. Another person writes a test script to make sure the function meets those requirements/specifications. Now, somewhere in the future someone decides to modify the square(n) function. Maybe they change what happens when there is an error. Now square(n) no longer behaves the way it was originally designed. That change could cause other areas in the application to break. This is why we write test scripts. Test scripts are a way to make sure everything behaves the way it was designed to behave. If you have a test script to test square(n) for all conditions and also tests all the other functions to work correctly as well, then you will know immediately that some functions are not working like they are designed to. Test scripts can test everything in your application and catch errors you might overlook otherwise. This test script makes sure square(n) handles good and bad data correctly. Imagine that across every function in your large application. It will catch direct errors as well as errors from functions which depend on other functions and so on. In small scripts like this it doesn't seem necessary. But in a larger application, it makes it easy to test every component in your app. Some errors are caused by user input. Some errors are caused because you modified the behavior of a function that other functions might use. Test scripts should test for every scenario. Then you can trust your app works after you've made changes to something. PyTest is designed to help you write those tests.
21st Oct 2024, 12:44 AM
Jerry Hobby
Jerry Hobby - avatar
+ 1
A file called calculator def main(): x = int(input("What's x? ")) print("x squared is", square(x)) def square(n): return n * n if __name__ == "__main__": main() ______________ A file to test import pytest from calculator import square def test_positive(): assert square(2) == 4 assert square(3) == 9 def test_negative(): assert square(-2) == 4 assert square(-3) == 9 def test_zero(): assert square(0) == 0 def test_str(): with pytest.raises(TypeError): square("cat") This my context Jerry Hobby . Anyway THX for your clarification But to make sure of my understanding Do you mean with pytest.raises (TypeError) it test the error that I want my function to catch when some error occurred by the user ?and Then I use a suitable try except statements to deal with that error to avoid runtime errors for user?
20th Oct 2024, 1:57 PM
Yusof
Yusof - avatar
+ 1
20th Oct 2024, 3:42 PM
Yusof
Yusof - avatar