1 / 18

COMPSCI 101 Principles of Programming

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

ely
Download Presentation

COMPSCI 101 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101Principles of Programming Lecture 05 – Testing Functions

  2. Learning outcomes • At the end of this lecture, students should be able to: • Use doctest to test functions COMPSCI 101 - Principles of Programming

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

More Related