Code # 1: Check that the function raised a ValueError
Code # 2 : need to check the value of the exception with a different approach
assertRaises () method provides a convenient way to check for an exception. A common mistake is writing tests that manually try to do something with exceptions on their own.
Code # 3: Example
The problem with such approaches is that it's easy to forget about corner cases, such as the case where no exceptions are thrown. To do this, you need to add an extra check for this situation as shown in the code below.
Code # 4:
assertRaises () method just takes care of these details, so it's preferred to use it. The only limitation of
assertRaises () is that it does not provide a means to validate the value of the generated exception object.
To do this, it must be manually checked. Somewhere in between these two extremes, you can use the assertRaisesRegex () method which allows you to check for an exception while matching the regular expression against the string representation of the exception.
Code # 5:
Little known fact about
assertRaises () and
assertRaisesRegex () is that they can also be used as context managers.
Code # 6:
This form can be useful if a test has multiple steps (such as customization) besides just executing the called one.
This book serves as a practical guide on how to utilize big data to store, process, and analyze structured data, focusing on three of the most popular Apache projects in the Hadoop ecosystem: Apache S...
Target knows. Apple Computer knows, too. So do LinkedIn, Netflix, Facebook, Twitter, Expedia, national and local political campaigns, and dozens of other organizations that all generate enormous eco...
The series “Studies in Big Data” (SBD) publishes new developments and advances in the various areas of Big Data-quickly and with a high quality. The intent is to cover the theory, research, develo...
Python Data Science Handbook: Essential Tools for Working with Data - PDF, 1st Edition For many researchers, Python is a first-class tool, primarily because of its libraries for storing, manipulati...