CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:

Slides:



Advertisements
Similar presentations
Software Testing. Quality is Hard to Pin Down Concise, clear definition is elusive Not easily quantifiable Many things to many people You'll know it when.
Advertisements

Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION DEBUGGING Autumn 2011 Bug-Date: Sept 9, 1947.
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
Program Correctness and Efficiency Chapter 2. Chapter 2: Program Correctness and Efficiency2 Chapter Objectives To understand the differences between.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
© The McGraw-Hill Companies, 2006 Chapter 9 Software quality.
Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
Program Correctness and Efficiency Chapter 2. Chapter Objectives  To understand the differences between the three categories of program errors  To understand.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Chapter 1 Principles of Programming and Software Engineering.
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
Software Testing Sudipto Ghosh CS 406 Fall 99 November 9, 1999.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
CSE219, Computer Science III Stony Brook University Test-Driven Development (a.k.a. Design to Test) 1.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Objectives Understand the basic concepts and definitions relating to testing, like error, fault, failure, test case, test suite, test harness. Explore.
© 2012 IBM Corporation Rational Insight | Back to Basis Series Chao Zhang Unit Testing.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
CMSC 345 Fall 2000 Unit Testing. The testing process.
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.
Chapter 13: Implementation Phase 13.3 Good Programming Practice 13.6 Module Test Case Selection 13.7 Black-Box Module-Testing Techniques 13.8 Glass-Box.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Java Coding Standards and Best Practices Coding Standards Introduction: After completing this chapter, you will able to keep your code up to standards.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
AP Computer Science Programming Conventions. Why coding conventions? 80% of the lifetime cost of a piece of software goes to maintenance. Hardly any.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 Introduction to Software Testing. Reading Assignment P. Ammann and J. Offutt “Introduction to Software Testing” ◦ Chapter 1 2.
What is Testing? Testing is the process of finding errors in the system implementation. –The intent of testing is to find problems with the system.
Code Conventions Tonga Institute of Higher Education.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
CPSC 873 John D. McGregor Session 9 Testing Vocabulary.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
Karlstad University Computer Science Design Contracts and Error Management External and internal errors and their contracts.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
SOFTWARE TESTING SOFTWARE TESTING Presented By, C.Jackulin Sugirtha-10mx15 R.Jeyaramar-10mx17K.Kanagalakshmi-10mx20J.A.Linda-10mx25P.B.Vahedha-10mx53.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
SOFTWARE TESTING LECTURE 9. OBSERVATIONS ABOUT TESTING “ Testing is the process of executing a program with the intention of finding errors. ” – Myers.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
CSE 219 Final exam review.
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.
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
CSE 219 Computer science III
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
CSE 1020:Software Development
Tonga Institute of Higher Education
Presentation transcript:

CSE 219 Computer Science III Testing

Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases: to minimize the chances of undetected bugs –...but you cannot always prove there are no bugs Debugging: Isolating fragments of the program that lead to incorrect behavior –Runtime errors –Logical errors

Important Definitions Testing –a process of running a program on a set of test cases and comparing the actual results with expected results Verification –a formal or informal argument that a program works as intended for all possible inputs Validation –a process designed to increase confidence that a program works as intended performed through verification or testing Defensive Programming –writing programs in a way designed to ease the process of validation and debugging

Kinds of Testing Unit Testing –Test each module in a program separately. Integration Testing –Test interfaces between modules. –Much more difficult than unit testing Regression Testing –Test programs after modifications to ensure correct behavior of the original program is preserved. System Testing –Test overall system behavior.

Aspects of Testing How do we generate test cases? –Exhaustive Consider all possible combinations of inputs. Often infeasible – why? Is it feasible with your program? –Sampled A small but representative subset of all input combinations. –Black-box testing - Test cases generated from program specifications and not dependent on the implementation –Glass-box testing - Test cases generated from program’s code

Black-box testing The best place to start when attempting to test a program thoroughly Test cases based on program’s specification, not on its implementation Test cases are not affected by: –Invalid assumptions made by the programmer –Implementation changes Use same test cases even after program structures has changed Test cases can be generated by an “independent” agent, unfamiliar with the implementation. Test cases should cover all paths (not all cases) through the specification, including exceptions.

Boundary Conditions A boundary condition is an input that is “one away” from producing a different behavior in the program code Such checks catch 2 common types of errors: –Logical errors, in which a path to handle a special case presented by a boundary condition is omitted –Failure to check for conditionals that may cause the underlying language or hardware system to raise an exception (ex: arithmetic overflow)

Glass-box testing Black-box testing is generally not enough For Glass-box testing, the code of a program being tested is taken into account Path-completeness: –Test cases are generated to exercise each path through a program. –May be insufficient to catch all errors. –Can be used effectively only for a program fragment that contains a reasonable number of paths to test.

Testing paths through specification Examine the method specifications (preconditions) & all paths through method to generate unique test cases for testing. Ex: /* REQUIRES: x >= 0 && y >= 10 */ public static int calc(int x, int y)... Translate paths to test cases: x = 0, y = 10 (x == 0 && y == 10) x = 5, y = 10 (x > 0 && y == 10) x = 0, y = 15 (x == 0 && y > 10) x = 5, y = 15 (x > 0 && y > 10) x = -1, y = 10 (x < 0 && y == 10) x = -1, y = 15 (x 10) x = -1, y = 9 (x < 9 && y < 10) x = 0, y = 9 (x == 0 && y < 10) x = 1, y = 9 (x > 0 && y < 10)

JUnit Unit-test framework for Java programs. –open source software –hosted on SourceForge –import junit.framework.*; –not in the standard JDK Associate a Test class with each unit –one or more classes The test class has a set of test methods –public void testX(), where X is the method to be tested The test methods use “assertions” to perform the tests, ex: –Assert.assertEquals(x,y) –Assert.assertTrue(c) –Assert.assertSame(obj1, obj2) –Etc …

Building unit tests with JUnit Initialize any instance variables necessary for testing in the test object Define tests for emptiness, equality, boundary conditions,... Define test suites, if necessary, to group tests. Use Assert methods to perform tests

JUnit Example – StatCompiler.java public class StatCompiler { /** * a, b, & c must all be positive **/ public static int averageOfPosInts(int a, int b, int c) throws IllegalArgumentException { if ((a < 0) || (b <0) || (c < 0)) throw new IllegalArgumentException("No neg values"); int sum = a + b + c; return sum/3; } public static int median(int a, int b, int c) { if ( (a >=b) && (a <=c))return a; else if ((a >= b) && (a >=c))return b; elsereturn c; }

StatCompilerTest.java import junit.framework.*; public class StatCompilerTest extends TestCase { public StatCompilerTest(java.lang.String testName) { super(testName); } public void testAverageOfPosInts() { System.out.println("testAverageOfPosInts"); Assert.assertEquals(StatCompiler.averageOfPosInts (1, 2, 3), 2); try{ StatCompiler.averageOfPosInts(-1, 2, 3); fail("Exception should have been thrown"); } catch (IllegalArgumentException iae) {} } public void testMedian() { System.out.println("testMedian"); Assert.assertEquals(2, StatCompiler.median(1, 2, 3)); Assert.assertEquals(2, StatCompiler.median(3, 2, 1)); }

StatCompilerTest.java (continued) testAverageOfPosInts testMedian =================================================== Errors logged for the StatCompilerTest test: No errors. =================================================== Failures logged for the StatCompilerTest test: Total failures: 1 Test case testMedian(StatCompilerTest) failed with "expected: but was: “ at StatCompilerTest.testMedian(StatCompilerTest.java:42) =================================================== Summary of StatCompilerTest test: Result: Failed Run: 2 Failures: 1 Errors: 0 Elapsed time: 0.01

Java Code Conventions Why have code conventions? –~80% of the lifetime cost of a piece of software goes to maintenance. –Hardly any software is maintained for its whole life by the original author. –Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. –If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create. Would you want to buy crappy code?

Java Recommendations No source files more than 2000 lines of code Order of appearance: –See: Class/interface documentation comment 2.class or interface statement 3.Class/interface implementation comment (/*...*/), if necessary 4.Class (static) variables 5.Instance variables 6.Constructors 7.Methods –Group these by functionality (those that work together)

More Conventions Avoid lines longer than 70 characters –not handled well by many terminals and tools. When an expression will not fit on a single line, break it according to these general principles: –Break after a comma. –Break before an operator. –Prefer higher-level breaks to lower-level breaks. –Align the new line with the beginning of the expression at the same level on the previous line. –If the above rules lead to confusing code or to code that's squished up against the right margin, just indent 8 spaces instead.

Declaration Conventions One declaration per line is recommended since it encourages commenting. In other words, int level; // indentation level int size; // size of table is preferred over int level, size; Do not put different types on the same line, Ex: int foo, fooarray[]; //WRONG!

Class & Method Conventions No space between a method name and the parenthesis "(" starting its parameter list Open brace "{" appears at the end of the same line as the declaration statement Closing brace "}" starts a line by itself indented to match its corresponding opening statement, –when it is a empty method the "}" should appear immediately after the "{" class Sample extends Object { int ivar1; int ivar2; Sample(int i, int j) { ivar1 = i; ivar2 = j; } int emptyMethod() {}... }

If, Loop, & Try/Catch conventions if (condition) { statements; } if (condition) { statements; } else { statements; } for (initialization; condition; update) { statements; } try { statements; } catch (ExceptionClass e) { statements; }

Additional Conventions Avoid using an object to access a class (static) variable or method. Use a class name instead. For example: classMethod(); //OK AClass.classMethod(); //OK anObject.classMethod(); //AVOID!