Chapter 15 Testing. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Why Testing? In chapter 14, we.

Slides:



Advertisements
Similar presentations
Testing Relational Database
Advertisements

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Programming Logic and Design, Third Edition Comprehensive
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Debugging Introduction to Computing Science and Programming I.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Chapter 1 Program Design
Chapter 3 Planning Your Solution
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
Object Oriented Programming
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
Introduction to Python
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
General Programming Introduction to Computing Science and Programming I.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Testing Michael Ernst CSE 140 University of Washington.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Programming Concepts Chapter 3.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSC 108H: Introduction to Computer Programming
Introduction to Computing Science and Programming I
Logger, Assert and Invariants
Testing UW CSE 160 Winter 2017.
Testing UW CSE 160 Spring 2018.
Exceptions 10-Nov-18.
Topics Introduction to File Input and Output
Conditions and Ifs BIS1523 – Lecture 8.
Testing UW CSE 160 Winter 2016.
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Throwing and catching exceptions
Loops CIS 40 – Introduction to Programming in Python
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
Presentation transcript:

chapter 15 Testing

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Why Testing? In chapter 14, we talked about protecting our programs from bad input, from users Who is going to protect us, the programmers, from ourselves (from writing programs with mistakes)? The answer is that we, the programmers, must protect ourselves. We are responsible for testing our programs

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. “Good” programs As much as possible, programmers need to integrate testing into programming. –how do you know if your program is right without tests to show that it is? There are a number of programming paradigms that require writing tests first, before a single line of code is written (test- driven programming)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Kinds of error Syntax: Errors of language use. Usually caught by Python Semantics: Syntax is correct, but we are asking Python to do something it cannot (divide by 0, convert ‘a’ to an integer, etc.) Design: Program doesn’t fulfill our expectations. No errors but wrong answers, incomplete results etc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Testing Helps Testing can help us avoid both semantic and design errors In so doing we can avoid errors, or if we still encounter them recover more quickly as the code is being developed, rather than having to rewrite it after the fact.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. design-test-program A good programmer should: –design their program before they write it. what functions, what classes, user interface etc. –write the test programs that the program should pass –then write the code proper design and testing makes writing code much easier, much clearer, less error prone

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. The term “bug” The term bug, at least as it applies to computing, is used to indicate an error in the program (of any kind) predates computing: –Edison used the term –Telegraphers and some of their specialized equipment Admiral Hopper had a real example!

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Poor choice of term The use of the term is disingenuous 1 The bug didn’t just “creep in” while we were sleeping! It was an error we made. It is our fault (the programmer), not some external force. We are responsible. 1 not candid or sincere, typically by pretending that one knows less about something than one really does.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Edsger Dijkstra on bugs “We could, for instance, begin with cleaning up our language by no longer calling a bug ‘a bug’ but by calling it an error. It is much more honest because it squarely puts the blame where it belongs, viz., with the programmer who made the error … While before, a program with only one bug used to be ‘almost correct,’ afterwards a program with an error is just ‘wrong.’ ”

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Kinds of testing

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Testing is hard! Before we go too far, it is important to note that testing is both a complicated area as well as a broad area (many aspects). We won’t be able to cover such a big topic in a short time, but we can give an overview. We encourage you to look deeper!

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. static vs. dynamic (1) Also described as planning versus testing of the code written to meet the plans –planning means understanding the problem and its requirements before you write the code. Shouldn’t change over time (though in real life it does), hence static. –testing of code means fixing what you wrote to implement the plan. The code is constantly updated, hence dynamic

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. post vs pre testing (2) When to test? Before or after coding? post testing is the traditional way, code it then test to make sure it runs pre testing is a more modern approach. Write the tests before the code, then make sure code updates work, and add to the test.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Levels of testing Unit testing. Test each individual unit (function, class, etc.) Integration testing. Do two units work together as designed. System testing. Does the overall system do what it is supposed to do.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Test What? This is probably the hardest part. Test what? This is really a topic unto itself. There are many aspects of a program that we might like to test Knowing the options is part of the problem.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Test What (1) Correctness. Are the results produced correct? What counts as correct? Do we know all the cases? Completeness. Are all the potential cases covered: all user entries, all file formats, all data types, etc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Test What (2) Security. Is the program safe from being broken into by another user, through the Internet, etc. Interface. Is the interface useable? Are its features complete? Does the user get what they expect when using the program?

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Test What (3) Load. Does the program respond well if it is presented with a heavy load: many users, large data sets, heavy Internet traffic etc. Resources. Does the program use appropriate amounts of memory, CPU, network?

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Test What (4) Responsiveness. Does the program respond in a reasonable amount of time. If there are time limits, does it meet those limits?

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Correctness, the Pentium story Is it possible that you can prove that your program is correct? Some researchers are doing just that, proving correctness Pentium chip of 1993 had a small flaw in its division algorithm (1 in 1x10 9 divisions). Never found in testing, showed up in the field for people doing research

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. NBA efficiency example

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Catching User Errors We know how to do this. Chapter 14, the discussion about exceptions, gives us the tool to write our program so we might protect it This gives us security, better usability and other factors.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 15-3

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Catch Developer Errors We need to protect ourselves from ourselves as well. Couple of ways we can check ourselves as we program –insert tests in the code for conditions which should never occur if the code is written correctly –add tests to the code to automatically check for test conditions provided by the programmer

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. The assert statement The assert statement has an associated Boolean clause. This clause is a condition that must always be True if the code is correct If the clause evaluates to False, then the program is halted.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. more assert If assert is triggered, it raises the builtin AssertionError assert can take a string, separated from the clause by a comma, to print out with the error

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 15-4

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. …

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. assert is for Programmers when calc_efficiency is called, it is not called by a user, nor does that function require user input the assumption is that what is passed to the function is already correct, as the programmer is making the invocation check to make sure we, the programmer, don’t make a mistake.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Inserting Tests

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. doctest doctest is a module provided by Python for module testing lets you describe (and then run) good usage examples for your code when code is changed, you can run those tests. also helps the reader/user to see what the designer’s intention is

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. doctest modifies the docstring in the docstring at the top of the module, you can place special tests each test is preceeded by a >>> just after the test is the exact result of having run that test each test is run and the output compared. If no problems, then the module ‘passes’ and no output is produced

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. testmod once written, you must call the doctest.testmod() function it looks in the file and examines the docstring for >>> runs each test and compares the answer if you run with a -v command line, all the tests will be compared otherwise, no output

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. can’t be too long cannot have the the docstring be too long as it becomes too much for the user however, if you are developing it is a convenient thing to run tests after each change short tests can then be used in conjunction with system tests

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 15.5

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. …

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. unittest module system testing is done using the unittest module unlike doctest, this is a separate file that describes in detail how to perform the overall test probably more useful, since it is not embedded in the sourcefile itself.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. more complicated, more features Basically, inherit from TestCase : every method that begins with ‘test’ is run setUp is run before every case tearDown is run after every case assertEqual checks for equal result assert_() to check a condition assertRaises to check for a raised exception