How to Test Methods Computer Science 3 Gerb Objective: Test methods properly.

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
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.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
Debugging Introduction to Computing Science and Programming I.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Program Design and Development
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Computer Science 1620 Programming & Problem Solving.
CS 201 Functions Debzani Deb.
30-Jun-15 Static Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
CS201 – C Functions Procedural Abstraction Code Reuse C Library.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Testing. Definition From the dictionary- the means by which the presence, quality, or genuineness of anything is determined; a means of trial. For software.
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Test Stubs... getting the world under control. TDD of State Pattern To implement GammaTown requirements I CS, AUHenrik Bærbak Christensen2.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Modularity Computer Science 3. What is Modularity? Computer systems are organized into components called modules. The extent to which this is done is.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Lazy Evaluation Computer Science 3 Gerb Objective: Understand lazy evaluation in Java.
Testing i. explain the importance of system testing and installation planning;
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
CSC 108H: Introduction to Computer Programming
Greenfoot.
The need for Programming Languages
Unit Testing.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Testing and Debugging.
Control Structures: Part 2
TRANSLATORS AND IDEs Key Revision Points.
BIT115: Introduction to Programming
Static Methods 14-Nov-18.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Threads and Multithreading
MSIS 655 Advanced Business Applications Programming
Coding Concepts (Sub- Programs)
Coding Concepts (Basics)
Computer Science Testing.
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
CS 240 – Advanced Programming Concepts
Javascript Chapter 19 and 20 5/3/2019.
Whitebox Testing.
Methods/Functions.
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Software Testing.
Presentation transcript:

How to Test Methods Computer Science 3 Gerb Objective: Test methods properly

Why Test Methods Modern software systems are built a piece at a time Often reuse pieces of one system for another Therefore, not enough to know that whole system works properly –Want to know each piece works properly –Want to be able to test a piece of the system –Smallest testable pieces are methods

What does it mean for a method to work properly? No matter how complex or simple, all methods receive inputs and produce outputs Inputs –Parameters –Input from keyboard, mouse, etc. Outputs –Return value –Output to screen A method works properly when it produces the correct outputs in response to its inputs

How can you tell whether a method is correct? Only one way Give it inputs Look at the outputs If the outputs are what you would expect given the inputs, you have reason to believe that the method works in that case

What to Test? Not good enough just to call once –Devise multiple tests –Vary the values of the inputs Strive for complete code coverage. I.e. each line is tested at least once Test boundary conditions –Test at least once on each side –Test at the boundary

For Example Writing a method to return true if a student is eligible for a scholarship award Specs –Freshmen are not eligible –Sophomores are eligible only with 3.5 GPA or better –Juniors and seniors are eligible with a 3.0 GPA or better

Example – What test cases? static boolean eligibleForAward(int grade, double gpa){ boolean eligible=true; if (grade==9) eligible = false; else if (grade==10){ if (gpa < 3.5) eligible = false; } else if (gpa < 3.0) eligible = false; return eligible;}

Example – What test cases? static boolean eligibleForAward(int grade, double gpa){ boolean eligible=true; if (grade==9) eligible = false; else if (grade==10){ if (gpa < 3.5) eligible = false; } else if (gpa < 3.0) eligible = false; return eligible;} 9 th grade 10 th grade –gpa < 3.5 –gpa > 3.5 –gpa = 3.5 Other –gpa < 3 –gpa > 3 –gpa = 3

How do you test Call the method Look at –Value returned by method –Anything normally output by method Never, never, never, never, never, never do: –Test by putting unnecessary prints in your method –You’re not testing what the method actually does –Possibility of misinterpreting results –Possibility of breaking function when you take them out

Don’t do this!!! static boolean eligibleForAward(int grade, double gpa){ boolean eligible=true; if (grade==9) eligible = false; else if (grade==10){ if (gpa < 3.5) eligible = false; } else if (gpa < 3.0) System.out.println(eligible); eligible = false; return eligible;}

Drivers Methods used to test other methods Avoid “throw away” tests –Changing your driver for each test or typing test data by hand –Makes retesting hard –Instead, add each new test to existing driver –Now can retest the entire module more easily Often a java class includes a main method that runs tests on the methods in the class

Summary Often want to test methods separately Create drivers for this purpose –Look at the method’s return value and output –Never, never, print from a methods for the purpose of testing it! Use multiple test cases No throwaway tests