Testing
When you complete the testing section of your project you need to make sure your testing table is specific and attempts to test all possible inputs.
Below are a series of programs and completed testing tables so you can see how you should complete the testing tables.
Example 1 - Email Address Validation
This program gets the user to enter an email address and it will decide if it is valid or not. The email address must follow the following format:
- start with a string of alphanumeric characters
- followed by the @ symbol
- another string of alphanumeric characters
- followed by a “.”
- then a string of alphanumeric characters
Program code:
import re
password = input("Enter an email address: ")
while len(password)==0:
password = input("Enter an email address: ")
pattern = "^[A-Za-z0-9]+\@[A-Za-z0-9]+\.[A-Za-z0-9]+$"
passwordMatch = re.match(pattern, password)
if passwordMatch:
print("VALID")
else:
print("INVALID")
Testing Table:
Testing Evidence:
Test 1 | |
---|---|
Test 2 | |
Test 3 | |
Test 4 | |
Test 5 |
Example 2 - Grade Calculator
This program gets the user to enter a test score between 0 and 100. It will then say the grade and how many marks they are off the next grade. The grade boundaries are:
- A = 70+
- B = 60+
- C = 50+
- D = 40+
- U = <40
Program code:
valid=False
while valid==False:
score = input("Enter a score between 0 and 100: ")
if score.isdigit()==False:
valid=False
else:
valid=True
if int(score)<0 or int(score)>100:
valid=False
score=int(score)
if score >=70:
marksoff = 100-score
print("That test score is a grade A, you were "+str(marksoff)+" marks off full marks")
elif score>=60:
marksoff = 70-score
print("That test score is a grade B, you were "+str(marksoff)+" marks off an A")
elif score>=50:
marksoff = 60-score
print("That test score is a grade C, you were "+str(marksoff)+" marks off a B")
elif score>=40:
marksoff = 50-score
print("That test score is a grade D, you were "+str(marksoff)+" marks of a C")
else:
marksoff=40-score
print("That test score is a grade U, you were "+str(marksoff)+" marks off a D")
Testing Table:
Testing Evidence:
Test 1 | |
---|---|
Test 2 | |
Test 3 | |
Test 4 | |
Test 5 |
Testing Table:
Testing Evidence:
Test 6 | |
---|---|
Test 7 | |
Test 8 | |
Test 9 | |
Test 10 |
Example 3 - Speeding Cars
This program reads a file that contains a list of car registration plates and the speed they were captured at going through a speed camera. The user will enter the speed limit and it will display which of the cars in the file were speeding. Part of the file cars.csv
is shown below:
Program code:
speed = input("Enter the speed limit (mph) ")
file = open("cars.csv","r")
speeding = False
for line in file:
cars = line.split(",")
if float(cars[1])>float(speed):
speeding = True
print(cars[0] + " " + cars[1])
file.close()
if speeding == False:
print("There were no cars that broke the speed limit of " + speed + "mph")
Testing Table:
Testing Evidence:
Test 1 | |
---|---|
Test 2 | |
Test 3 |
Example 4 - Order Program
This program reads a file that contains a list of products and their prices sold in a shop. The user enters a GTIN code (barcode) and how many the customer wants. It then calculates the total cost of each item and a grand total and creates a receipt.
CSV File
Program code:
anotherItem=True
receipt=""
totalcost=0
while anotherItem is True:
gtin = input("Enter a GTIN8 Number: ")
quantity = input("Enter a quantity: ")
file=open("Products1.csv","r")
itemFound=False
for line in file:
product=line.split(",")
if product[0]==gtin:
price=float(product[2])
itemFound=True
itemTotal=round(price*int(quantity),2)
receipt=receipt+"\n"+product[0]+" "+product[1]+" "+quantity+" "\
+"£{:.2f}".format(price)+" "+"£{:.2f}".format(itemTotal)
print("Current Order")
print(receipt)
totalcost=totalcost+itemTotal
file.close()
if itemFound==False:
receipt=receipt+"\n"+gtin+" product not found"
print(receipt)
continueOrder=input("Order another item (y/n)?")
if continueOrder=="y":
anotherItem=True
else:
anotherItem=False
print("Order complete")
receipt=receipt+"\n"+"Total cost of order £{:.2f}".format(totalcost)
print(receipt)
Testing Table:
Testing Evidence:
Test 1 | |
---|---|
Test 2 | |
Test 3 | |
Test 4 |
Example 5 - Reorder Program
This program reads a file that contains a list of products and how many there are in stock. If the number in stock is low, it works out how many need to be reordered and then creates a new file of the products that need to be reordered as well as how many.
Program code:
print("List of products that are below the reorder level and need to be reordered:")
itemsforreorder=0
file=open("catalogue - T3.csv","r")
for line in file:
product=line.split(",")
if int(product[3])<int(product[4]):
reorderamount=int(product[5])-int(product[3])
print(product[0]+" "+product[1]+" "+str(reorderamount))
reorderfile=open("reorderlist.csv","a")
reorderfile.write(product[0]+","+product[1]+","+str(reorderamount)+"\n")
reorderfile.close()
itemsforreorder=itemsforreorder+1
file.close()
if itemsforreorder>0:
print("Reorder file created")
else:
print("No products need reordering")
Testing Table:
Testing Evidence:
Test 1 | |
---|---|
Test 2 | |
Test 3 |
Example 6 - Updating Staff Information
This program updates the number of years a member of staff has been working at the school. When the program runs the user enters their email address, when it finds the person it asks them to enter their number of years teaching. It will then update the file if the person exists in the file.
Program code:
import os, sys
updateanother = "Y"
while updateanother == "Y":
email = input("Enter the email address you are looking for:")
file = open("staff.csv","r")
alreadyexists=False
for line in file:
staff = line.split(",")
writefile = open("staffupdated.csv","a")
if staff[2] == email:
alreadyexists=True
print("Current Details:\nStaff name: " + staff[0] + " " + staff[1] + "\nEmail Address: " + staff[2] + "\nYears Teaching: " + staff[3])
yearsteaching =input("Enter their new number of years teaching: " )
writefile.write(staff[0] + "," + staff[1] + "," + staff[2] + "," + yearsteaching+"\n")
else:
writefile.write(staff[0] + "," + staff[1] + "," + staff[2] + "," + staff[3])
writefile.close()
file.close()
os.remove("staff.csv")
os.rename("staffupdated.csv","staff.csv")
if alreadyexists==False:
print("That staff member's email cannot be found in the file, no changes made")
else:
print("Details updated")
updateanother = input("Would you like to update another staff member? Y/N ")
Testing Table:
Testing Evidence:
Test 1 | |
---|---|
Test 2 | |
Test 3 |