Testing Object Oriented Programs CSE 111 4/28/20151.

Slides:



Advertisements
Similar presentations
Testing Relational Database
Advertisements

Giving a formal meaning to “Specialization” In these note we try to give a formal meaning to specifications, implementations, their comparisons. We define.
Integration Testing When testing a module in isolation –Called modules need to be replaced –Tested module needs to be called A D ′ E ′ main driver module.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Copyright W. Howden1 Lecture 7: Functional and OO Design Descriptions.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Object-Oriented PHP (1)
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
Copyright W. Howden1 Lecture 8: O/O Programming. Copyright W. Howden2 Topics OO Programming Languages Developing programs from Designs –Class and method.
Copyright W. Howden1 Lecture 11: UML Terminology and Additional Models and Notation.
Copyright W. Howden1 Lecture 13: Programming by Contract.
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
Copyright W. Howden1 Lecture 15: Generalization, Polymorphism and States.
Testing an individual module
Lecture a: Additional UML Models: Package, Activity, Deployment Lecture b: Generalization, Aggregation and Additional Domain Model Notation Copyright W.
ASP.NET Programming with C# and SQL Server First Edition
Lecture 7: UML Class Diagrams CSE 111 7/15/20151Copyright W. Howden.
Introduction to Software Testing
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Object Oriented Programming CEN 221. Course Description Classes, objects, inheritance, polymorphism, graphical user interfaces, event handling, exception.
Systems Analysis and Design in a Changing World, Fifth Edition
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Lecture 6 Software Testing and jUnit CS140 Dick Steflik.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
CS 501: Software Engineering Fall 1999 Lecture 16 Verification and Validation.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
Chapter 8 – Software Testing Lecture 1 1Chapter 8 Software testing The bearing of a child takes nine months, no matter how many women are assigned. Many.
Automated GUI testing How to test an interactive application automatically?
12 Systems Analysis and Design in a Changing World, Fifth Edition.
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
Ch6: Software Verification. 1 Decision table based testing  Applicability:  Spec. is described by a decision table.  Tables describe:  How combinations.
1 ITEC 3010 “Systems Analysis and Design, I” LECTURE 10: Use Case Realizations [Prof. Peter Khaiter]
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Chapter 13: Regression Testing Omar Meqdadi SE 3860 Lecture 13 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Systems Analysis and Design in a Changing World, 3rd Edition
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 22 Slide 1 Software Verification, Validation and Testing.
Software Construction Lecture 18 Software Testing.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
(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.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
Design Model Lecture p6 T120B pavasario sem.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
Unit Testing. F-22 Raptor Fighter Manufactured by Lockheed Martin & Boeing How many parts does the F-22 have?
Dynamic Testing.
Outsourcing, subcontracting and COTS Tor Stålhane.
OOP Basics Classes & Methods (c) IDMS/SQL News
INF230 Basics in C# Programming
Testing Tutorial 7.
Designing For Testability
About the Presentations
Introduction to Software 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.
COP 3330 Object-oriented Programming in C++
Applying Use Cases (Chapters 25,26)
Designing For Testability
Agenda Software development (SD) & Software development methodologies (SDM) Orthogonal views of the software OOSD Methodology Why an Object Orientation?
Presentation transcript:

Testing Object Oriented Programs CSE 111 4/28/20151

2 Traditional Stages in Testing Unit testing Test individual classes or small groups of classes during development Integration testing Test larger groups of classes and subsystems System testing Test the whole system. Use test scripts that describe a sequence of user- program interactions and expected resulting behavior 4/28/2015

Traditional Testing (and Analysis) Methods Functional or black box testing: test with normal and oddball cases. – normal: expected functionality – oddball: invalid inputs, empty arrays, etc. Coverage: makes sure all the program (e.g. branches, variables and data structures) has been used on a test Code reading: read the code, mentally simulating what it is supposed to be doing 4/28/20153

Specific Object-Oriented Techniques Self-testing classes Test case classes Stubs, drivers and mock objects Class design features that facilitate testing – i.e. design for testability Subsystem testing strategy for 3-tier system Inheritance and abstract test cases 4/28/20154

5 Self-Testing Classes Each class has a main method that is used for testing Except for the class with the “real” main method that is used to start the program Testing a class A with its main() method: Create an instance of the class A Call its methods, checking the results Report results of tests 4/28/2015

6 Test Case Class Special class T for testing another class or classes (the CUT = class under test) T contains a collection of special test and verify methods main() method in test case class T – Creates an instance of the CUT, performs initialization, runs its test methods 4/28/2015

7 Tests and Test Suites Test = TestCase or TestSuite TestSuite = Collection of Tests 4/28/2015

8 TestRunners Run the tests for a set of classes Needs to be able to know which methods in a class are the test methods it needs to run If test methods use special assert statements to check results, then results are automatically reported 4/28/2015

J-Unit Originally was far more complex and flexible, but was difficult to use JUnit 4 – Special annotation is used to indicate which method is a test method may be in the CUT or in separate test case classes – Supports specialized kinds of assert statements and other advanced, simplifying features 4/28/20159

Sample JUnit Test Case import static org.junit.Assert.assertEquals; public class AdditionTest { private int x = 1; private int y = public void addition() {int z = x + y; assertEquals(2, z); } 4/28/201510

Stubs and Drivers Suppose we want to test a class C which supports some responsibility, but is called by other classes, and calls classes itself How to isolate C during testing? – Create a “driver” test case class whose test methods call methods in C. The test methods contain assert statements that check the results of the tests – Create dummy “stub” objects that emulate the classes called by C. Do not need to be a full implementations, just have enough logic to handle the calls resulting from the tests 4/28/201511

12 Class Design - Testability Designing classes to assist in testing – Mock objects and parameterized class constructors 4/28/2015

13 Mock Objects and Parameterized Constructors 1 O/O equivalent of a stub is called a “mock object” – Suppose the Class Under Test (CUT) uses instances of another, “auxiliary” class – Suppose that the auxiliary class is not available, or want to test CUT in isolation – Need to a create an instance of a mock object class M which stands in for or simulates instances of the actual class 4/28/2015

14 Mock Objects and Parameterized Constructors 2 Suppose a constructor for class A takes an instance of class B – Define the constructor parameter using an interface C, which B implements (“interface” in the Java sense) – This means that instances of alternative classes can be passed, as long as the classes implement interface C. e.g. a matching mock object class M 4/28/2015

15 Mock Objects and Parameterized Constructors 3 Suppose that objects of class A send messages to an object of class B. In the design, could use attribute visibility, in which, for example, the constructor for A creates an instance of B and assigns it to a class variable in A Facilitate testing with a design that uses parameter visibility instead where the constructor for A has a parameter for passing an instance of B which is created “outside” of A Now follow the approach of the previous slide, using an interface to declare the parameter type so that either instances of B or mock objects can be passed 4/28/2015

DS Example The Domain Logic subsystem has to send messages to the DataBase subsystem (i.e. to the DB Java interface class) Instead of creating the DataBase subsystem instance “inside of” DomainLogic, “pre-create” the DataBase (interface) instance and pass it as an interface-typed parameter in the DomainLogic constructor Permits easy testing of DomainLogic with a stub/mock object for the DataBase, and seamless transference to use of a real DataBase instance 4/28/201516

17 JUnit and Mock Objects Suppose class A uses an object of class B, and this is passed in the constructor for A, with a parameter of type C In a test case class which tests A – inside its test method(s) create a mock object of type C create an instance of the CUT (in this case B) under test, and pass the mock object in the constructor for B – requires that the C be an interface type 4/28/2015

Test Strategies for Our Three-Tier System Integration testing – Separate testing of GUI, DomainLogic and DataBase subsystems – Subsystems small, so may simply do integration testing for each subsystem, with no unit testing DataBase – test DB directly, test DB through the DL DomainLogic – test DL directly, using mock DB, use test cases simulate the GUI GUI – manual testing with DL mock object – possible separate testing of GUI classes using “information objects” 4/28/201518

19 DataBase Testing Why not simply test the DB in the context of the whole system? Typical test case would – Set up or check DB status – Perform application methods – Check effects by accessing DB 4/28/2015

20 Problems with Whole System - Strategy for Testing the DataBase Want to test DB directly to observe effects of DB method calls Want directly examine the effects of “complex” sequences of DB method calls that would occur as the result of DL activity Need to directly test the correctness of the interfaces between DB methods and the DL methods that call them 4/28/2015

21 Testing the DB Directly - Individual Method Applications Test the DB by writing test cases that execute methods in the DB interface Use black box test selection, backed up by coverage analysis, for each DB interface method DB interface may have only a small number of methods, which we can focus on individually 4/28/2015

Testing the DB Directly - Method Sequences Problems may occur due to sequences of DB method applications BET (bounded exhaustive testing) – test exhaustively over all inputs with a “small version” of the application – e.g. DS. Only has 2-3 members, sequences of user interactions limited to length, say, 3 4/28/201522

Sample DB Bug in DS Example Sequence of interactions used to reveal bug 1. initialize() /*reads in files from DB, examples with 0,1,2 entries 2. delete(x) where x is not a member of the DB Note: this defect is not discoverable if a deletion of a non-existent member is performed after a deletion of an existing member has been performed. So simple testing of a non-existing member may not work. BET picks up the strange short sequence that causes the failure 4/28/201523

24 Testing the DomainLogic/DB Interfaces Purpose is to test the links from one to the other – ignore functionality for now – write test cases that call methods in the DL (Java) interface, which result in calls from DomainLogic class methods to the DB (Java interface) – focus on testing each of these DL/DB interfaces for consistency, as simply and directly as possible Note: you may find interface inconsistencies during focused test construction 4/28/2015

25 Test Case Classes for Testing the DB Directly TestCase contents use a class variable for DB class instance setup() method would create a DB instance and assign to the DB instance variable (this is an empty DB) tearDown() method deletes everything from DB testx() methods would create and retrieve entries from data base, using DataBase (Java interface) methods and method sequences. Assertions in the test methods will reference data returned from DB accesses 4/28/2015

Test Case Classes for Testing DomainLogic/DB Interfaces Test Case contents – use a class variable for DB class instance – setup() method would create an instance dB of the DB (Java interface) and possibly use dB methods to populate the data base – testx() methods would create an instance of DomainLogic, passing dB as a constructor method parameter execute DomainLogic methods use DB methods directly to check results – teardown() removes effects of tests 4/28/201526

Domain Logic Testing Test the methods in the DL interface using black box and coverage testing measures – include coverage testing of called methods in the DL – focus on DL (Java interface) methods Simulate the GUI – test methods would have (sequences of) calls on DL that simulate a GUI action 4/28/201527

28 Test Case Classes for Domain Logic Testing 1)setUp() method in the test case could: 1)create an instance of the mock DB, 2)create an instance of the DL, passing the mock DB as a parameter. 2)testx() method could 1)execute DL methods, checking results with an assert() 4/28/2015

29 GUI Testing How do we test the GUI? – We will use manual GUI testing only – Test cases will create an instance of a mock DL and pass it as a parameter in the constructor for the GUI – Need to construct scripts that document tester input and expected results for each test case Checking results: tester inspection via GUI 4/28/2015

Separate Testing of GUI Classes Suppose that we want to test a GUI class more directly, by itself, but it calls on/creates other GUI classes? – If other GUI classes simply present results, keep them because they are the test output – Suppose that the other GUI classes accept input and return information How to delink these subordinate GUI objects? 4/28/201530

31 Delinking GUI Classes Suppose that a window A creates a dialog B for gathering some information Instead create a Java interface class Info with a method getInfo() – getInfo() returns an information object for test use, define an implementation of Info where getInfo() sets and returns an information object directly for production use, define an implementation of Info where getInfo() creates an instance of B, reads the information, and then returns it in an information object 4/28/2015

Setting the Info object Two possibilities – The GUI object A has a constructor with a parameter for passing in the Info object – requires an external class, to construct the subclass implementation instances of Info for test or production runs – The GUI class A can be abstract with an abstract factory method create() – Subclass A with a definition of create() that uses the correct implementation constructor, depending on whether A is being used for testing or production use 4/28/201532

Test Strategies for Our Three-Tier System System Testing – Combine DomainLogic and with a real DB instances, simulate actions of GUI can also be classified as a higher level of integration testing – Test whole system through the GUI – apply black box, BET coverage and code reading are not usually done at the system level 4/28/201533

DS System Testing Example When doing BET sequence testing included the operations of terminating and starting a new DS session In the DS if you logon as a member, and then ask to edit your data, and do not enter any new properties, and then terminate a session, you will get a crash when you try to restart the system with a new session 4/28/201534

35 New Topic: Abstract Test Cases Suppose that a class C’ subclasses a class C – Can we develop a test case T for C that we can re- use for C’? – Consider the case where C is an interface class – In general, only some tests be reused, and there is obvious way to determine which 4/28/2015

36 Creating Abstract Test Cases To create an abstract test, construct an abstract test class T so that it uses an abstract factory method createC() to create instances of C If C’ is a subclass of C, which implements the interface, then create a subclass T’ of T in which the implementation of the abstract factory method createC() uses the constructor for C’ to create an concrete instance of C (i.e. instance of C’) 4/28/2015

37 Polymorphism and TestCases Polymorphic design pattern: – A set of classes corresponds to related alternative forms of behavior – Can capture alternatives in a set of subclasses of a class – Subclasses have a common set of methods, but exhibit different behavior depending on the subclass in which they are defined 4/28/2015

38 Creating Abstract Polymorphic Test Cases Use approach similar to above, for inheritance Usefulness? Depends on how much logic in the abstract test case methods is reusable for the subclasses 4/28/2015

39 Additional Test Frameworks: HttpUnit HttpUnit emulates the relevant portions of browser behavior, including form submission, JavaScript, basic http authentication, cookies and automatic page redirection, and allows Java test code to examine returned pages either as text, an XML DOM, or containers of forms, tables, and links. When combined with a framework such as JUnit, it is fairly easy to write tests that very quickly verify the functioning of a web site.JavaScript http authenticationJUnit 4/28/2015

Assignment 8 Use the principles in the lecture to design tests for your project – Use J-Unit to construct and document 2 test cases for 2 classes. Use normal and oddball tests. – Construct system level tests as per earlier system testing slide (33) for user inputs use normal and oddball cases use BET to test short sequences of operations 4/28/201540