180 likes | 357 Views
COMPSCI 101 Principles of Programming. Lecture 05 – Testing Functions. Learning outcomes. At the end of this lecture, students should be able to: Use doctest to test functions. Recap. Functions are defined using the key word def Accept arguments (parameters) Return values
E N D
COMPSCI 101Principles of Programming Lecture 05 – Testing Functions
Learning outcomes • At the end of this lecture, students should be able to: • Use doctest to test functions COMPSCI 101 - Principles of Programming
Recap Functions are defined using the key worddef • Accept arguments (parameters) • Return values • Functions are a way to generalise tasks • Defining a function extends the tasks that are available • Design functions to be reused as much as possible • Provide documentation so other programmers know how to use the function • Code tracing with functions • A box is drawn to represent the function • Variables drawn inside the box to indicate they are not visible outside the function COMPSCI 101 - Principles of Programming
How do you know if your code works? • Syntax errors • Easy to identify • Static analysis possible • The compiler tells you • Runtime errors • Occur while the program is running • Provide feedback about when the program caused the error • Often harder to fix than syntax errors but easier than logic errors • Logic errors • Difficult to identify • Program does exactly what you told it • Not always what you meant COMPSCI 107 - Computer Science Fundamentals
Expensive Fireworks (1996) • In 1996, code from the Ariane 4 rocket is reused in the Ariane 5, but the new rocket’s faster engines trigger a bug in an arithmetic routine inside the flight computer. • The error is in code to convert 64-bit floating-point numbers to a 16-bit signed integers. The faster engines cause the 64-bit numbers to be larger, triggering an overflow condition that crashes the flight computer. • As a result, the rocket's primary processor overpowers the rocket's engines and causes the rocket to disintegrate only 40 seconds after launch. COMPSCI 107 - Computer Science Fundamentals
Example • Write a function that calculates the area of a square • Arguments (input) • Return value (output) • Docstring (explain what the function does) defsquare_area(side): """Returns the area of a square Arguments: side – length of the side (float or int) Returns: The area of the square """ return side ** 2 COMPSCI 101 - Principles of Programming
Test cases • Before you write the code, figure out what output you expect • Example: Write a function that calculates the area of a square COMPSCI 107 - Computer Science Fundamentals
Example: Convert weight from kg to lbs defpounds_from_kilo(kilograms): """Converts a weight from kilograms to pounds Arguments: kilograms - weight in kg (float) Returns: Weight in pounds (float) Note: conversion rate is 1 kg = 2.20462 lbs """ return kilograms * 2.20462 COMPSCI 101 - Principles of Programming
Test cases • Before you write the code, figure out what output you expect • Example: Write a function that converts kgs to lbs • 1 kg = 2.20462 lbs COMPSCI 107 - Computer Science Fundamentals
Exercise • Write a function that calculates the area of a triangle. • How can you check if your code works correctly? COMPSCI 107 - Computer Science Fundamentals
Test cases • Before you write the code, figure out what output you expect • Example: Write a function that calculates the area of a triangle COMPSCI 107 - Computer Science Fundamentals
doctest • doctest is a simple system for testing code • Not as sophisticated as unit testing • Tests are included in the docstring for a function • Any line that begins with the Python prompt >>> will be executed • The output from executing the code will be compared with the line following • To test the code, include the following statements in the module >>> circle_area(1) 3.1415927 include this inside your docstring for the circle_area function import doctest doctest.testmod() COMPSCI 107 - Computer Science Fundamentals
Example deftriangle_area(base, height): """Returns the area of a triangle. Arguments: base -- a number (float) or (int) representing the length of the triangle base height -- a number (float) or (int) representing the length of the triangle height Returns: The area of the triangle (float) >>> triangle_area(10, 5) 25.0 >>> triangle_area(1, 1) 0.5 >>> triangle_area(2.5, 2) 2.5 """ return base * height / 2 COMPSCI 107 - Computer Science Fundamentals
Example • Add tests to the function that calculates the area of a square defsquare_area(side): """Returns the area of a square Arguments: side – length of the side (float or int) Returns: The area of the square >>> square_area(10) 100 >>> square_area(5.5) 30.25 """ return side ** 2 COMPSCI 101 - Principles of Programming
Exercise • Add doctest tests to the docstring for this function defpounds_from_kilo(kilograms): """Converts a weight from kilograms to pounds Arguments: kilograms - weight in kg (float) Returns: Weight in pounds (float) Note: conversion rate is 1 kg = 2.20462 lbs """ return kilograms * 2.20462 COMPSCI 101 - Principles of Programming
Example • Write a function that takes a base cost for an item (in dollars, excluding GST) and returns the cost for an item including GST (in dollars, to the nearest cent). • Examples of output: >>> add_gst(6) 6.9 >>> add_gst(0.06) 0.07 >>> add_gst(7) 8.05 >>> add_gst(10) 11.5 >>> add_gst(100) 115.0 >>> add_gst(10.01) 11.51 COMPSCI 101 - Principles of Programming
Example: Adding GST defadd_gst(before_tax): """Returns the cost with GST added Arguments: before_tax - the cost before tax (i.e. excluding GST) Returns: The cost of the item including GST. The value is rounded to the nearest cent. >>> add_gst(100) 115.0 >>> add_gst(10.01) 11.51 """ after_tax = before_tax * 1.15 #GST is 15% return round(after_tax, 2) COMPSCI 101 - Principles of Programming
Summary • Tests are important to know whether the code works! • Doctest is a simple way to automate the tests • Anywhere in the docstring, include tests • Tests are simply lines that start with >>> • Results of the test are compared with the following line(s) COMPSCI 101 - Principles of Programming