Reference no: EM133087568
Lab - Testing, Error Handling and Debugging
Exercises
Question 1. In your own words, explain what black box testing and white box testing are - including the major difference between them.
Question 2. When Unit Testing our code, we use a combination of Test Cases and Unit Tests. Describe what each of these three terms mean, including the difference between test cases and unit tests.
Question 3. You are provided with the following Python code for a function that adds two numbers. Copy and paste the code into a .py document, import the unittestmodule, and then write a test case consisting of two unit tests that pass and two unit tests that fail.
defsum_values(first, second):
"""Calculate the sum of two values and return the result."""
the_sum = first + second
return the_sum
Once you have your passing and failing unit tests and have run them, modify the function so that all the unit tests pass! There may be a variety of ways to do this which will depend on details of the failing unit tests that you wrote - for example, there may be too many or too few parameters provided, or the data types might not match etc. Fix the code so that it passes your unit tests as you see fit.
Question 4. A check digit is a single digit in a larger value that is used to determine whether the value as a whole has not been modified or corrupted and is valid. Lots of things have check digits embedded in them to ensure the number provided is legal - for example bar codes on products, credit card numbers, tracking numbers on parcels etc.
You are provided with a class called UniversalProductCode which calculates the check digit of a value using the following process:
a) Add the digits in the odd-numbered positions (first, third, fifth, etc.) together and multiply by three.
b) Add the digits (up to but not including the check digit) in the even-numbered positions (second, fourth, sixth, etc.) to the result.
c) Take the remainder of the result divided by 10 (modulo operation) and if not 0, subtract this from 10 to derive the check digit
The code for the class is provided below (either copy and paste it into a file called upc.py or download a version from the Moodle shell for this weeks' lab):
Now that we have a class to test, create a upc_test.py file in which you should write a TestUniversalProductCode class that uses Python's unit test module to test that various product codes are valid or invalid, and that providing data in the wrong format (i.e. a product code that contains letters is not valid). Take a look at slides 24 to 30 of this weeks' lecture to help get you started.
You can look up some valid universal product codes (for example, here's some for Cadbury chocolate bars - but you can search for anything: add a zero at the beginning if valid codes looked up say they are not valid!).
In your TestUniversalProductCode class you should write a test that checks for a few different valid and invalid UPC codes, and modify the UniversalProductCode class to raise an exception instead of crashing when provided with illegal data (i.e. ‘OneTwoThree' is illegal because it doesn't contain only digits).
Use the unittestsetUp(self)method to provide a number of UPC codes in a list (see slide 32 for details on how to use the setUp method, and slides 34 onwards for details on exception handling).
For the above three UPCs, you should use the assertTrue, assertFalseand assertRaisesassertions to ensure the first (valid) code is accepted as valid, the second isn't, and the third throws an exception.
Question 5. In your own words, explain why you should use logging to help debug your programs instead of using the print statement?
Also, write a small program that uses Python's logging module to log five different log messages, each with different levels of importance (debug, info, warning, error, critical).
Question 6. Our final activity for today will be to experiment with the Python debugger, which is called pdb.
Next, click the "Clone or download" button and then select "Download ZIP". Save the pdb-tutorial.zip file to the desktop and extract it.
Attachment:- Error Handling and Debugging.rar