COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Similar documents
COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Errors. And How to Handle Them

Python for Informatics

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Testing. UW CSE 160 Winter 2016

CS558 Programming Languages

CS558 Programming Languages

The compiler is spewing error messages.

Data Structures. Lists, Tuples, Sets, Dictionaries

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

Intro. Scheme Basics. scm> 5 5. scm>

Lecture 9: July 14, How to Think About Debugging

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

Testing and Debugging

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

Reliable programming

Text Input and Conditionals

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

CS558 Programming Languages

COMP1730/COMP6730 Programming for Scientists. Functions

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

MITOCW watch?v=9h6muyzjms0

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Fundamentals of Programming. Lecture 1: Introduction + Basic Building Blocks of Programming

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Algorithm Design and Recursion. Search and Sort Algorithms

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

Introduction to Problem Solving and Programming in Python.

CS Lecture 26: Grab Bag. Announcements

Beyond Blocks: Python Session #1

1 Getting used to Python

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Introduction to Python (All the Basic Stuff)

Lecture. Algorithm Design and Recursion. Richard E Sarkis CSC 161: The Art of Programming

CS 3 Introduction to Software Engineering. 3: Exceptions

Programming in the Real World. Dr. Baldassano Yu s Elite Education

QUIZ. What is wrong with this code that uses default arguments?

12. Logical Maneuvers. Topics: Loop-Body Returns Exceptions Assertions Type Checking Try-Except

Lecture 4 CSE July 1992

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

PROGRAMMING FUNDAMENTALS

Chapter 9: Dealing with Errors

The Environment Model. Nate Foster Spring 2018

Errors and Exceptions. EECS 230 Winter 2018

An Introduction to Python

Lecture 9: GUI and debugging

Reminders. Lecture Outline. Lecture Agenda. Lecture Outline. Remember This? COMP10001 Foundations of Computing Testing and Debugging

Python Tutorial. CS/CME/BioE/Biophys/BMI 279 Oct. 17, 2017 Rishi Bedi

COMP10001 Foundations of Computing Testing and Debugging

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

EXAMINING THE CODE. 1. Examining the Design and Code 2. Formal Review: 3. Coding Standards and Guidelines: 4. Generic Code Review Checklist:

Try and Error. Python debugging and beautification

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Cosmology with python: Beginner to Advanced in one week. Tiago Batalha de Castro

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Racket Style Guide Fall 2017

A Foundation for Programming

4.2 Function definitions the basics

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Lecture 21. Programming with Subclasses

MEIN 50010: Python Flow Control

CMSC 201 Computer Science I for Majors

Python for Non-programmers

Pointers (continued), arrays and strings

CSCA08 Winter Week 12: Exceptions & Testing. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Variables, expressions and statements

How to approach a computational problem

Functions and Decomposition

The Environment Model

The second statement selects character number 1 from and assigns it to.

Testing. Prof. Clarkson Fall Today s music: Wrecking Ball by Miley Cyrus

Types, Expressions, and States

Pointers (continued), arrays and strings

Implementing an Algorithm for Boomerang Fraction Sequences in Python

Python for Everybody. Exploring Data Using Python 3. Charles R. Severance

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Getting Started with Python

Design Approaches for Concurrent Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 02/09/2012

Conditionals: Making Choices

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Programming with Python

Upcoming Features in C# Mads Torgersen, MSFT

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Python Unit Testing and CI Workflow. Tim Scott, Python Meetup 11/07/17

DECOMPOSITION, ABSTRACTION, FUNCTIONS

Peer-To-Peer Network!

Transcription:

COMP1730/COMP6730 Programming for Scientists Testing and Debugging.

Overview * Testing * Debugging * Defensive Programming

Overview of testing * There are many different types of testing - load testing, integration testing, user experience testing, etc. * We are concerned with unit-testing or functional testing. * Usually done at the function (or method level). * Done by calling a function with specified parameters and checking that the return value is as expected.

Unit-testing in Python * There are many ways to do unit-testing in Python. The simplest is Python s unittest module. import unittest class Test(unittest.TestCase): def test function a(self): self.assertequals(function a( x, y, z), expected value)

Test methods * assertequals(a, b): Test whether expression a and b are equal. * asserttrue(a): Test whether expression a is True. * assertisnone(a): Test whether expression a evaluates to None. * assertin(x, xs): Test whether x is an element in collection xs.

Tips for unit-testing * Have your tests in a separate file. * A small function is easier to test than a large function. * A function that only does one thing is easier to test than a function that does many things. * Unit-testing is only concerned with the outputs of a function (and occasionally side-effects). Don t try and test how a function does its thing. * Especially true when testing class methods (not really covered in this course).

The Debugging Process 1. Detection - realising you have a bug. 2. Isolation - narrowing down the cause. 3. Comprehension - Working out what went wrong. 4. Correction - Fixing the problem. 5. Prevention - Making sure the bug can t happen again. 6. Go back to step 1.

Syntax Errors * The code is not valid python code. * These are usually the easiest type to find because you can t run the code until you resolve them. * Python usually tells you where they are (approximately).

Runtime Errors * The code is valid Python code - but it s being used to do something Python doesn t know how to do. * Causes an exception when run (possibly only under certain conditions). * Learn to read (and understand) Python s error messages. ZeroDivisionError is largely self-explanatory, but understand what causes Python to raise an AttributeError.

Semantic Errors (Logic Errors) * The code is valid Python code and runs without error, it just does the wrong thing (possibly only sometimes). * To detect these type of bugs, you must have a good idea of what the code is supposed to do. * These are usually the hardest type of bug to detect and fix, particularly if they only occur under certain conditions.

Working out what went wrong * Work back from where a bug appears (e.g. the line number for an error message). * Run the code with simpler inputs that still exhibit the bug, e.g. only use the first few records in a data set. * Add print statements to view the state of variables. * Use unit-tests to rule out functions that are working correctly. Be careful though, since if the bug only occurs under certain conditions, these conditions need to be tested in the unit-test suite.

Some Common Bugs * Logical operations are not English. * Loop condition is not modified. * Off-by-one errors. * Floating point precision. * is vs ==.

Defensive Programming Everyone knows that debugging is twice as hard as writing a program in the first place. So if you re as clever as you can be when you write it, how will you ever debug it? Brian Kernighan

Code Quality Matters! * A function that is hard to read is hard to debug. def AbC(ABc): ABC = len(abc) ABc = ABc[ABC-1:-ABC-1:-1] if ABC == 0: return 0 abc = AbC(ABc[-ABC:ABC-1:]) if ABc[-ABC] < 0: abc += ABc[len(ABc)-ABC] return abc

Pre and Post Conditions * Functions allow for breaking larger programs into small pieces which can be separately tested and debugged. * assert statements allow us to ensure that only appropriate parameters are passed as arguments to functions. Example: assert type(param a) == int and param a > 0 * Unit tests allow us to verify that the function is returning the appropriate value for the given inputs.

Explicit vs Implicit * Make things explicit if they are unclear or could be confusing. Even if they are working as intended. * return None is better than no return statement. * - (2 ** 2) instead of - 2 ** 2. * (a and b) or c instead of a and b or c. * dict() instead of { }.

Avoid Language Tricks * Don t make use of language quirks in your code. * Example: operator chaining. >>> 1 == 2 False >>> False is not True True >>> 1 == 2 is not True???