Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”

Slides:



Advertisements
Similar presentations
Automating Software Module Testing for FAA Certification Usha Santhanam The Boeing Company.
Advertisements

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.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Programming Types of Testing.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
JUnit, Revisited 17-Apr-17.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
The Java Programming Language
Lecture 2: Classes and Objects, using Scanner and String.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Errors “Computer says no..”. Types of Errors Many different types of errors new ones are being invented every day by industrious programming students..
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSC Programming I Lecture 6 September 4, 2002.
Unit Testing CSIS 3701: Advanced Object Oriented Programming.
Decisions, Decisions, Decisions Conditional Statements In Java.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
5.01 Understand Different Types of Programming Errors
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
1 Phase Testing. Janice Regan, For each group of units Overview of Implementation phase Create Class Skeletons Define Implementation Plan (+ determine.
Compilers and Interpreters
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Information and Computer Sciences University of Hawaii, Manoa
5.01 Understand Different Types of Programming Errors
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Testing Tutorial 7.
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Chapter 10 – Exception Handling
Class Structure 15-Jun-18.
CS1101X Programming Methodology
CompSci 230 Software Construction
Java Primer 1: Types, Classes and Operators
Java Programming Language
Testing and Debugging.
Loop Structures.
Chapter 3: Using Methods, Classes, and Objects
Data types and variables
CS 240 – Lecture 11 Pseudocode.
Control Structures - Repetition
5.01 Understand Different Types of Programming Errors
Introduction to C++ Programming
slides created by Ethan Apter
Programming Fundamentals (750113) Ch1. Problem Solving
Arrays in Java What, why and how Copyright Curt Hill.
Part B – Structured Exception Handling
slides created by Ethan Apter
Program Correctness and Efficiency
Bugs & Debugging - Testing
elementary programming
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
Review of Previous Lesson
LCC 6310 Computation as an Expressive Medium
slides created by Ethan Apter and Marty Stepp
CHAPTER 6 Testing and Debugging.
Presentation transcript:

Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration” testing Systems – “system” testing Testing can only show that errors do exist; it cannot guarantee that they do not exist – E. Djikstra

People who test Programmers do unit tests to ensure classes do what they are supposed to do Dedicated testers may do unit testing, integration testing and system testing In real-world, testing often done by someone other than programmer. In class, you are it! The best time to perform testing is at the same time you are coding while the logic of the code is fresh in your mind.

Categories of Errors Compilation errors – occur during compile Run-time errors – cause execution to abort Logic errors – cause computed results to be incorrect

Compilation Errors Syntax errors – grammatical mistakes missing semicolon on statement Unmatched parenthesis No comma between method parameters If problem is bad enough, it may cause cascade of errors, because compiler becomes confused. One error could result in the compiler reporting many errors. Semantic errors – code has no meaning Incrementing an int variable before it is initialized Using a variable before it is declared Assigning a String to an int variable Semantic errors may indicate a lack of understanding of the meaning of the programming language constructs being used. X=5 (3+5 myStr = s.substring(0 1); int i; i++;

Run-time Errors Ex: Using method on null reference Name n = Name.read(br); //null if not read if (n.getFirst().equals(”Sammy”)) count++; If Name.read returns a null, then cannot do getFirst method on null reference Exception is thrown and program aborts Fix errors by process of debugging Another example: int x = Integer.parseInt( myBufRdr.readLine() ); int y = 10/x;

Finding Run-Time Errors Java is helpful when an exception is thrown In console window you will see Exception in thread “main” java.lang.NullPointerException at Account.write(Account.java:45) at Program2.solution(Program2.java:75) at Program2.main(Program2.java:8) Tells you method, class, file, line number where exception was thrown This is probably not where the fault is located cascade

Logic Errors Programmer made a mistake Either didn’t understand what was supposed to happen, or made a typographical error Examples: class DoubleTrouble { public int doubleIt() { value += 2; //mistake, want to multiply return value; } private int value; } //comparing two reference variables Boolean sameName = (emp1 == emp2); //when in actuality the programmer wished to compare the //two strings in the string objects, (not the references) Boolean sameName = emp1.equals(emp2);

System Testing Can test a whole program by giving it different values for input Goal is to choose test input that are both valid and invalid and see if system behaves as you would expect. Choose input values by looking at problem statement – called “black box” testing

Testing Classes Test Driver A method with purpose of testing a class. Often make a static method of class being tested Partially automates testing of class Code as a method in the class so the class contains its own test code.

Example Test Driver class Employee { //… public static void testDriver() { Employee e = new Employee(”Gerald Weiss”, 25); int hours = 36; System.out.println(”Name: ” + e.getName()); System.out.println(”Rate: ” + e.rate()); System.out.println(”Hours: ” + hours); System.out.println(”Pay: ” + e.calcPay(hours)); } Test code is a method in the class so that class carries around its own test code.

Automating Testing public static void testDriver() { Employee e = new Employee(”Gerald Weiss”, 25); int hours = 36; int correctAnswer = 900; //computed by hand System.out.println(”Name: ” + e.getName()); System.out.println(”Rate: ” + e.rate()); System.out.println(”Hours: ” + hours); int computedPay = e.calcPay(hours); System.out.println(”Pay: (computed)” + computedPay); }

Automating Testing // continued from previous page if (computedPay != correctAnswer) { System.out.print(”****Error: computed pay should be”); System.out.println(correctAnswer); } else System.out.println(”Test successful”);

Getting More Information Helper class makes writing test drivers easier class TestHelper { public static void verify(boolean testCond, String message) { if (!testCond) System.out.print(”****Error – test failure: ”); System.out.println(message); Thread.dumpStack(); } Thread.dumpStack() aborts program and prints exception trace information to indicate where error occurs (see slide 7)

Designing Tests Test methods and constructors Don’t just test “most important” methods Test in logical order test from most primitive to more complex very effective to test all possible pairs of sequences Execute each statement at least once (“line coverage”, “statement coverage”) Can also test each condition (“condition coverage”, “branch coverage”)

Choosing Test Values Test special cases special values: zero, null, empty string boundary conditions: 40 is a boundary condition for condition hours <= 40 (Also try “near” boundary values, like 39 and 41.) Do not worry about efficiency of test, so don’t worry about how many you choose (Testing is not concerned with speed or efficiency)

Debugging Process of discovering faults Already know program has errors First look for method responsible for computation Use output statements to view values of variables. Place statements in particular places to see what values are printed. Compare hand computation with output statements.