C07: Testing and JUnit

Similar documents
Manuel Oriol, CHCRC-C, Software Testing ABB

Introduction to CS 270 Math Foundations of CS

Announcements. Testing. Announcements. Announcements

Warm-Up Problem. Let be a set of well-formed Predicate logic formulas. Let be well-formed Predicate logic formulas. Prove or disprove the following.

Testing. UW CSE 160 Winter 2016

C02: Overview of Software Development and Java

Program Verification. Aarti Gupta

Software Testing Lecture 1. Justin Pearson

Program Validation and Testing

CS 520 Theory and Practice of Software Engineering Fall 2018

Introduction & Formal Methods

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

Administrivia. ECE/CS 5780/6780: Embedded System Design. Acknowledgements. What is verification?

Static program checking and verification

CSE 403: Software Engineering, Fall courses.cs.washington.edu/courses/cse403/16au/ Unit Testing. Emina Torlak

Topics in Software Testing

Summer May 11, 2010

Testing and Debugging Lecture 11

CISC : Finite-State Verification

Writing better code Loop invariants Correctness. John Edgar 2

Principles of Software Construction: Objects, Design, and Concurrency (Part 2: Designing (Sub )Systems)

C08: Inheritance and Polymorphism

System Correctness. EEC 421/521: Software Engineering. System Correctness. The Problem at Hand. A system is correct when it meets its requirements

Testing: Coverage and Structural Coverage

CMSC 132: OBJECT-ORIENTED PROGRAMMING II

Verifying Parallel Programs

Intro to Proving Absence of Errors in C/C++ Code

Announcements. Lab tomorrow. Quiz Thursday P3 due Friday. Exceptions and unit testing

Chapter 11, Testing. Using UML, Patterns, and Java. Object-Oriented Software Engineering

linaye/gl.html

An Eclipse Plug-in for Model Checking

18-642: Unit Testing 1/31/ Philip Koopman

CS/ECE 5780/6780: Embedded System Design

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 15 Testing

Testing. CMSC 433 Programming Language Technologies and Paradigms Spring A Real Testing Example. Example (Black Box)?

F. Tip and M. Weintraub FUNCTIONAL TESTING

Verification and Test with Model-Based Design

C09: Interface and Abstract Class and Method

Hybrid Verification in SPARK 2014: Combining Formal Methods with Testing

Software Engineering and Scientific Computing

Software Testing part II (white box) Lecturer: Giuseppe Santucci

Static Analysis Techniques

C27a: Stack and Queue

Aerospace Software Engineering

Test-Driven Development (a.k.a. Design to Test) CSE260, Computer Science B: Honors Stony Brook University

Testing, Debugging, and Verification

C08: Inheritance and Polymorphism

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Smart Test Case Quantifier Using MC/DC Coverage Criterion

Software Quality. What is Good Software?

CS159. Nathan Sprague. September 30, 2015

CMPT 473 Software Quality Assurance. Graph Coverage. Nick Sumner

Unit Testing. CS 240 Advanced Programming Concepts

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Who is our rival? Upcoming. Testing. Ariane 5 rocket (1996) Ariane 5 rocket 3/8/18. Real programmers need no testing!

To be or not programmable Dimitri Papadimitriou, Bernard Sales Alcatel-Lucent April 2013 COPYRIGHT 2011 ALCATEL-LUCENT. ALL RIGHTS RESERVED.

Outline. software testing: search bugs black-box and white-box testing static and dynamic testing

EECS 4313 Software Engineering Testing. Topic 01: Limits and objectives of software testing Zhen Ming (Jack) Jiang

CITS5501 Software Testing and Quality Assurance Formal methods

10. Software Testing Fundamental Concepts

C18a: Abstract Class and Method

C06: Java API & Libraries

Software Engineering I (02161)

Topics. Software Testing Test Driven Development Black Box Testing Unit Testing White Box Testing Coverage Testing Software Debugging

Testing! Prof. Leon Osterweil! CS 520/620! Spring 2013!

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

Testing: Coverage and Structural Coverage

Chapter 7 Control Statements Continued

Tutorial 3: Unit tests and JUnit

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Testing. ECE/CS 5780/6780: Embedded System Design. Why is testing so hard? Why do testing?

Testing: Test design and testing process

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Reasoning about programs

Formal methods What are they? Uses Tools Application to software development

Lecture 20: SW Testing Presented by: Mohammad El-Ramly, PhD

Verification and Validation of Models for Embedded Software Development Prashant Hegde MathWorks India Pvt. Ltd.

Why testing and analysis. Software Testing. A framework for software testing. Outline. Software Qualities. Dependability Properties

CIS 890: Safety Critical Systems

Chapter 9 Quality and Change Management

C10: Garbage Collection and Constructors

(See related materials in textbook.) CSE 435: Software Engineering (slides adapted from Ghezzi et al & Stirewalt

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

Programmieren II. Unit Testing & Test-Driven Development. Alexander Fraser.

Pearson Education 2007 Chapter 9 (RASD 3/e)

Examining the Code. [Reading assignment: Chapter 6, pp ]

Lecture1: Symbolic Model Checking with BDDs. Edmund M. Clarke, Jr. Computer Science Department Carnegie Mellon University Pittsburgh, PA 15213

MTAT : Software Testing

Program Verification! Goals of this Lecture! Words from the Wise! Testing!

Feasibility of Testing to Code. Feasibility of Testing to Code. Feasibility of Testing to Code. Feasibility of Testing to Code (contd)

Software Quality Assurance. David Janzen

WHITE PAPER. 10 Reasons to Use Static Analysis for Embedded Software Development

Introduction to Dynamic Analysis

Tuesday, November 15. Testing

18-642: Unit Testing 9/18/ Philip Koopman

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

Correctness of specifications. Correctness. Correctness of specifications (2) Example of a Correctness Proof. Testing versus Correctness Proofs

Software Engineering

Model Checking. Automatic Verification Model Checking. Process A Process B. when not possible (not AI).

Programming Embedded Systems

Transcription:

CISC 3120 C07: Testing and JUnit Hui Chen Department of Computer & Information Science CUNY Brooklyn College 9/19/2017 CUNY Brooklyn College 1

Outline Recap and issues Grades and feedback Assignments & projects Review guide & CodeLabs Testing Junit Assignments Practices 9/19/2017 CUNY Brooklyn College 2

Recap and Issues Any questions? Grades and feedback Assignments & projects Review guide & CodeLabs 9/19/2017 CUNY Brooklyn College 3

Software Failures Ariane 5 rocket explosion (June 4, 1996) Overflow from conversion from a 64-bit floating point number to a 16-bit signed integer value Therac-25 lethal radiation overdose (June 1985 ~ Jan 1987) Some basic software engineering principles were apparently violated Mars Climate Orbiter disintegration (December 11, 1998)... In the case of the ground software, was in English units of pounds (force)-seconds (lbf-s) rather than the metric units specified FBI Virtual Case File project abandonment (2000 ~ 2005) a systematic failure of software engineering practices 9/19/2017 CUNY Brooklyn College 4

Recent Stories in the Airlines Industry The big computer systems that get airplanes, passengers and baggage to their destinations every day are having a bad summer. (NY Times, August 8, 2016) 1,000 0f 6,000 Delta flights canceled, August 8-9, 2016 2,300 canceled flights over 4 days at Southwest Airlines, ~July 22, 2016 Hundreds of flights grounded at United Airlines, July 2015 An ipad software glitch caused two days of problems for American Airlines, the airline said Wednesday 9/19/2017 CUNY Brooklyn College 5

Software Quality Assurance Verification Did you build the thing right? (Did you meet the specification?) Validation Did you build the right thing? (Is this what the customer wants? That is, is the specification correct?) Two approaches Testing Formal methods 9/19/2017 CUNY Brooklyn College 6

Formal Methods for Software Verification Start with a formal specification and prove code behavior follows that of specification Mathematical proofs Humans do the proofs Computers do the proofs Automatic theorem proving Model checking In practice, mostly done for hardware; done in very limited cases for software Computational intensive: hard to test, not too large spec., error repair cost prohibitive, critical components or systems 9/19/2017 CUNY Brooklyn College 7

In 1981, Edmund M. Clarke and E. Allen Emerson, working in the USA, and Joseph Sifakis working independently in France, authored seminal papers that founded what has become the highly successful field of Model Checking. This verification technology provides an algorithmic means of determining whether an abstract model--representing, for example, a hardware or software design-- satisfies a formal specification expressed as a temporal logic formula. Moreover, if the property does not hold, the method identifies a counterexample execution that shows the source of the problem. The progression of Model Checking to the point where it can be successfully used for complex systems has required the development of sophisticated means of coping with what is known as the state explosion problem. Great strides have been made on this problem over the past 27 years by what is now a very large international research community. As a result many major hardware and software companies are now using Model Checking in practice. Examples of its use include the verification of VLSI circuits, communication protocols, software device drivers, real-time embedded systems, and security algorithms. -- by ACM, 2007 9/19/2017 CUNY Brooklyn College 8

Testing for Software Verification 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 Programming testing can be used to show the presence of bugs, but never to show their absence! Edsger W. Dijkstra 9/19/2017 CUNY Brooklyn College 9

Testing Exhaustive testing is infeasible e.g., 1 nanosecond for test a program that has one 64-bit input, how long does it take to test all possible input values? Reduce the space Perform different tests at different phases of software development 9/19/2017 CUNY Brooklyn College 10

Different Types of Tests System testing (acceptance testing): if the integrated program meets its specification Integration testing: interfaces between units have consistent assumption and communicate correctly Module testing: tests across individual units (e.g., across classes) Unit testing: single method does what was expected (e.g., within a single class) System Testing (Acceptance Testing) Integration Testing Module Testing Unit Testing 9/19/2017 CUNY Brooklyn College 11

Perspectives: Black-box vs. White-box Tests Black-box tests Test design is solely based on the program s external specifications White-box (glass-box) tests Test design reflects knowledge about the program s implementation, e.g., developers doing the tests For it or against it? 9/19/2017 CUNY Brooklyn College 12

Test Coverage The fraction of the possible program execution paths that have been tested 100% of test coverage is no guarantee of design reliability Quality of tests also matters 9/19/2017 CUNY Brooklyn College 13

Common Test Coverage Levels S0 (method coverage) Is every method executed at least once by the test suite? S1 (call coverage or entry/exit coverage) Has each method been called from every place it can be called? C0 (statement coverage) Is every statement of the source code executed at least once by the test suite? C1 (branch coverage) Has each branch been taken in each direction at least once? C2 (path coverage) Has every possible route through the code been executed? 9/19/2017 CUNY Brooklyn College 14

Sample Code to Test 1. public class MyClass { 2. public void foo(boolean x, boolean y, boolean z) { 3. if (x) 4. if (y && z) bar(0); 5. else 6. bar(1); 7. } 8. public boolean bar(x) { 9. return x; 10. } 11. } 9/19/2017 CUNY Brooklyn College 15

Test Coverage S0 1. public class MyClass { 2. public void foo(boolean x, boolean y, boolean z) { 3. if (x) Satisfying S0 requiring calling foo and bar at least once each in the tests 4. if (y && z) bar(0); 5. else 6. bar(1); 7. } 8. public boolean bar(x) { 9. return x; 10. } 11. } 9/19/2017 CUNY Brooklyn College 16

Test Coverage S1 1. public class MyClass { 2. public void foo(boolean x, boolean y, boolean z) { 3. if (x) Satisfying S1 requiring calling bar from both line 4 and line 6 in the test suites 4. if (y && z) bar(0); 5. else 6. bar(1); 7. } 8. public boolean bar(x) { 9. return x; 10. } 11. } 9/19/2017 CUNY Brooklyn College 17

Test Coverage C0 1. public class MyClass { 2. public void foo(boolean x, boolean y, boolean z) { 3. if (x) 4. if (y && z) bar(0); 5. else 6. bar(1); Counting both branches of a conditional as a single statement, satisfying C0 requiring calling foo at least once with x true, and at least once with y false 7. } 8. public boolean bar(x) { 9. return x; 10. } 11. } 9/19/2017 CUNY Brooklyn College 18

Test Coverage C1 1. public class MyClass { 2. public void foo(boolean x, boolean y, boolean z) { 3. if (x) 4. if (y && z) bar(0); Satisfying C1 requiring calling foo at least once with x true, and with x false, and with y && z true and false. 5. else 6. bar(1); 7. } 8. public boolean bar(x) { 9. return x; 10. } 11. } 9/19/2017 CUNY Brooklyn College 19

Test Coverage C2 1. public class MyClass { 2. public void foo(boolean x, boolean y, boolean z) { 3. if (x) Satisfying C2 requiring calling foo with all 8 combinations of values of x, y, and z 4. if (y && z) bar(0); 5. else 6. bar(1); 7. } 8. public boolean bar(x) { 9. return x; 10. } 11. } 9/19/2017 CUNY Brooklyn College 20

Modified Condition/Decision Coverage (MCDC) Combines a subset of the above levels Each point of entry and exit in the program have been invoked at least once Every decision in the code has taken all possible outcomes at least once Each condition in a decision has been shown to independently affect that decision s outcome 9/19/2017 CUNY Brooklyn College 21

Achieving Test Coverage 100% of C0 coverage is not unreasonable. Achieving C1 coverage requires careful construction of tests. C2 is the most difficult of all, and the additional value of 100% of C2 is debatable. 9/19/2017 CUNY Brooklyn College 22

Questions? Examples of catastrophic software failures Software quality assurance A few important concepts on software testing 9/19/2017 CUNY Brooklyn College 23

Team Discussion & Exercise Design tests for a method 9/19/2017 CUNY Brooklyn College 24

Unit and Functional Testing Recall Unit testing: single method does what was expected (e.g., within a single class) Functional testing: a well-defined subset of the code does what was expected (e.g., several methods and classes) Tests Calls to methods with different input parameters Asserts on the effects of method calls Aims for high coverage Almost always white-box, and performed by developers 9/19/2017 CUNY Brooklyn College 25

Test Assertion An expression encapsulates some testable logic about a target under test 9/19/2017 CUNY Brooklyn College 26

JUnit A unit testing framework for Java Test assertion in JUnit It throws an exception if it evaluates to false 9/19/2017 CUNY Brooklyn College 27

Unit Test Example with JUnit Some of you completed the Array and ArrayList assignment Let s use it as an example Test whether the delete method functions as specified. public class FruitArray {... } public void delete(string fruitname) { } 9/19/2017 CUNY Brooklyn College 28

What is the Specification? public class FruitArray {... public void delete(string fruitname) { } } 9/19/2017 CUNY Brooklyn College 29

What is the Specification? Remove an element, shrink the list, as if the element had never been in the list. public class FruitArray {... } public void delete(string fruitname) { } 9/19/2017 CUNY Brooklyn College 30

What is the Test Assertion? It is expected that upon an item is deleted, the object should be identical to another object of the class that does not have the item from beginning with, but has all the other items. 9/19/2017 CUNY Brooklyn College 31

Example Design Add methods to aid testing Some methods may be added just for testing purpose. But, you don t want to break the good design just for unit tests! Testable code tends to be good code, and vice versa. 9/19/2017 CUNY Brooklyn College 32

Example Implementation in JUnit 4 Use JUnit 4, assume Maven project in Eclipse A few steps Update pom.xml <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> <scope>test</scope> </dependency> Remove AppTest.Java Create your own test class (See https://github.com/junit-team/junit4/wiki/getting-started) Complete the example in ArrayArrayList in the sampleprograms repository 9/19/2017 CUNY Brooklyn College 33

FruitArrayTest.java import static org.junit.assert.assertequals; import static org.junit.assert.assertarrayequals; import org.junit.test; public class FruitArrayTest { @Test // Important: annotate a test public void testdelete() { testdeletebydeleting1st(); testdeletebydeleting3rd(); testdeletebydeletinglast(); private void testdeletebydeleting1st() { String[] fruits = {new String("Apple"), new String("Banana"), new String("Kiwi"), new String("Mango"), new String("Orange")}; FruitArray fruitarray = new FruitArray(fruits); fruitarray.delete(new String("Apple")); assertequals(fruitarray.getsize(), fruits.length - 1); assertequals(fruitarray.getcapacity(), fruits.length); assertarrayequals(fruits, fruitarray.getfruitsasarray()); } testdeleteignorecasebydeleting1st(); testdeleteignorecasebydeletinglast(); } Note: the purpose of the test is to show how you write tests in a JUnit. This test is actually poorly designed. See the sample code in the repo for better ones. 9/19/2017 CUNY Brooklyn College 34

Did We Pass the Tests? 9/19/2017 CUNY Brooklyn College 35

Questions JUnit Examples in JUnit 4 Update pom.xml Use the sample code as the template or the one in the JUnit 4 wiki page 9/19/2017 CUNY Brooklyn College 36

Assignments How are we doing? Projects Assignments Review questions and CodeLab 9/19/2017 CUNY Brooklyn College 37