unit testing pg

24
Programming Techniques Module 2: Fundamental Programming Concepts 2.1_FundamentalProgrammingConcepts.ppt 1 IDCGRF001-A Copyright © 2009 Accenture All Rights Reserved. Module 1: Fundamentals of Unit Testing Unit Testing Copyright © 2009 Accenture All Rights Reserved. Accenture, its logo, and Accenture High Performance Delivered are trademarks of Accenture. Course Code #IDCGRF001-A Definition of Unit Testing Overview of Unit Testing Concepts Contents Unit Testing in V-Model Purpose and Scope of Unit Testing Techniques and Environment Benefits of Unit Testing Unit Testing Deliverables Copyright © 2009 Accenture All Rights Reserved. 2 Limitations of Unit Testing

Upload: raghav-jha

Post on 15-Feb-2016

16 views

Category:

Documents


0 download

DESCRIPTION

Unit testing is very good.Please read and learn it.

TRANSCRIPT

Page 1: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

1IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Module 1: Fundamentals of Unit Testing

Unit Testing

Copyright © 2009 Accenture All Rights Reserved. Accenture, its logo, and Accenture High Performance Delivered are trademarks of Accenture.Course Code #IDCGRF001-A

• Definition of Unit Testing• Overview of Unit Testing Concepts

Contents

g p• Unit Testing in V-Model• Purpose and Scope of Unit Testing• Techniques and Environment• Benefits of Unit Testing• Unit Testing Deliverables

Copyright © 2009 Accenture All Rights Reserved. 2

• Limitations of Unit Testing

Page 2: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

2IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Contents• Writing Test Conditions/Test Cases• Coverage Types in Unit Testing

Branch CoverageBranch CoverageCondition CoverageLoop CoverageInterface CoverageLogic Path Coverage

• Test condition design for Database Operations

Copyright © 2009 Accenture All Rights Reserved.

• Test condition design for GUI• Regression Test• Performance Test

3

Module Objectives• At the end of this module, you will be able to:

– Define Unit Testing– Describe the purpose and benefits of unit testingp p g– Identify the general principles of test conditions– Apply and utilize the principles of test conditions

Copyright © 2009 Accenture All Rights Reserved. 4

Page 3: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

3IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Definition of a Unit• In procedural programming, Unit may be

• an individual program or • a function or• a procedure, etc.

• In object oriented programming, the smallest unit is a class.

• Unit may be called as a Component

Copyright © 2009 Accenture All Rights Reserved. 5

Definition of a Unit Testing• Validates the source code of a defined functional unit.

• Helps a Software Developer to :• Helps a Software Developer to :– Test code for correctness– Design better (i.e., more cohesive ,less coupled code)– Re-factor code faster– Enhance documentation– Provide means for performance regression testing

Copyright © 2009 Accenture All Rights Reserved.

• Is a proactive approach to code.

6

Page 4: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

4IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Overview of Unit Testing Concepts• In Java, and other object-oriented languages, a unit is a class.• Testing is a way of evaluating software, to determine if

requirements and expectations are met and to detect errorsrequirements and expectations are met, and to detect errors.• Unit testing validates individual units of source code to check if

they are working properly.• Unit testing is usually done by the developer, during the build

phase of the application.• Although the nature of unit testing may have similarities with

debugging, they must not be confused with each other.

Copyright © 2009 Accenture All Rights Reserved.

gg g, y• Unit Test Script or Unit Test Case (popularly called as UTC) is

the first step in Build phase where a Developer needs to prepare the cases before coding.

7

Unit Testing in the V-Model

Unit Testing in V-Model

Copyright © 2009 Accenture All Rights Reserved. 8

Page 5: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

5IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

• Purpose of Unit Testing– Ensures that code is consistent with the detailed design– Ensures that the program can execute normally

Purpose of Unit Testing

– Ensures that the program can execute normally– Early detection of bugs– Prepares for future test phases

• Unit Test should cover– Code– Branches

Copyright © 2009 Accenture All Rights Reserved.

Branches– Paths– Cycles

9

• Scope of Unit Testing

For newly generated/developed code

Scope of Unit Testing

– For newly generated/developed code• All units/components of the code

– For changed/modified code• All the affected units/components of the code along with the

units/components that were directly changed

Copyright © 2009 Accenture All Rights Reserved. 10

Page 6: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

6IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

• The technique usually used for unit testing is white-box testing.

• White box testing is a testing strategy wherein the internal

Techniques and Environment

• White-box testing is a testing strategy wherein the internal workings of a unit/component are studied to create test conditions.

• The test environment should be similar to the real environment as much as possible.

Copyright © 2009 Accenture All Rights Reserved.

• There are tools such as Junit which are used for Unit Testing.

11

• Benefits of Unit Testing– Facilitates Change– Simplifies Integration

Benefits of Unit Testing

– Simplifies Integration– Provides living documentation– Serves as a design

• Unit testing Phases – Detailed Design Phase– Coding Phase

Copyright © 2009 Accenture All Rights Reserved.

Coding Phase– Unit Test Phase

12

Page 7: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

7IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

• Unit Testing Deliverables– Test Conditions and Expected Results– Test Cycle Control Sheet

Unit Testing Deliverables (cont.)

– Test Cycle Control Sheet– Test Data– Test Drivers / Media Programs Document– Test Environment Document

Copyright © 2009 Accenture All Rights Reserved. 13

DeliverablesDetailed Design

PhaseCoding Phase

Unit Test Phase

Unit Testing Deliverables

Test Conditions and Expected Results Created Updated Updated

Test Cycle Control Sheet Created Updated Updated

Test Data Document Created Updated Updated

T t D i / M di P

Copyright © 2009 Accenture All Rights Reserved.

Test Driver / Media Program Document Created Updated Updated

Test Actual Result Created

Test Environment Document Created Updated Updated

14

Page 8: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

8IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

• Limitations of Unit Testing– Checks only the functionality of units; unable to identify system-wide

issues

Limitations of Unit Testing

issues– Exposes only the presence of errors, not their absence– Combinatorial problems require significant amount of test code to be

written to cover all combinations– Needs rigorous sense of discipline throughout the software

development process– More effective when used in conjunction with other software testing

activities

Copyright © 2009 Accenture All Rights Reserved.

activities

15

Writing Test Conditions• A test condition is a statement that defines a constraint that

must be satisfied by the program being tested.

• Test Condition General Principles– Branch Coverage– Condition Coverage– Loop Coverage– Interface Coverage– Logic Path Coverage

Copyright © 2009 Accenture All Rights Reserved.

Logic Path Coverage

16

Page 9: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

9IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Branch Coverage• Branch Coverage

– Test conditions should be developed to cover all logic branches in the function modules. This ensures that no executable statementsthe function modules. This ensures that no executable statements are missed.

– Some conditions that may belong in this category are• IF-ELSE-ENDIF• CASE-WHEN-ENDCASE• WHILE-ENDWHILE

Copyright © 2009 Accenture All Rights Reserved. 17

Branch Coverage Example

Copyright © 2009 Accenture All Rights Reserved. 18

Page 10: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

10IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Condition Coverage• Condition Coverage

– Test conditions should be developed to make sure that every possible value of every judgment can be executed at least once.possible value of every judgment can be executed at least once.

– Test conditions that are used are boundary conditions usually for conditions that allow ranges.

– IF-ELSE conditions and its combinations belong in this category.

Copyright © 2009 Accenture All Rights Reserved. 19

Condition Coverage – Example 1• Design enough conditions to make sure every possible value of

every judgment condition can be executed at least once.

Copyright © 2009 Accenture All Rights Reserved. 20

Page 11: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

11IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Condition Coverage – Example 2• Condition A OR (Condition B and Condition C)• A total of 8 data combinations need to be tested

Copyright © 2009 Accenture All Rights Reserved. 21

Condition Coverage – Example 3• Condition A OR (Condition B and Condition C)• Take condition B and C as a single condition M, then the

formula becomes:formula becomes:– “Condition A or Condition M”

Copyright © 2009 Accenture All Rights Reserved. 22

Page 12: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

12IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Condition Coverage – Example 4

Copyright © 2009 Accenture All Rights Reserved. 23

Condition Coverage – Example 5

Copyright © 2009 Accenture All Rights Reserved. 24

Page 13: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

13IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Condition Coverage – Example 6• Get the boundary conditions.

Copyright © 2009 Accenture All Rights Reserved. 25

Loop Coverage• Loop Coverage

– Test conditions should be developed to cover all possible cases, including the loop being executed only once, or multiple times, orincluding the loop being executed only once, or multiple times, or executed maximum times, or even the loop never being executed.

– Loop statements such as the FOR, WHILE, DO-WHILE belong in this category.

Copyright © 2009 Accenture All Rights Reserved. 26

Page 14: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

14IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Loop Coverage – Example

Copyright © 2009 Accenture All Rights Reserved. 27

Interface Coverage• This is in unit testing scope.• Ensure the interface for individual program unit work correctly.

The factors to be considered when testing the Interface include :– Quantity, type and dimension for both real and formal parameters– Sequence of parameters in modules– Changing the value of read only parameters– Consistency of Global variable definitions in different modules.– Pay special attention to reference parameters

Copyright © 2009 Accenture All Rights Reserved.

Pay special attention to reference parameters

28

Page 15: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

15IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Logic Path Coverage• All Logic paths should be tested during Unit Test

Copyright © 2009 Accenture All Rights Reserved. 29

Other Types of Coverage

1. Exception Handling Test2. Argument Type Cast Test3. Comparison Computing Test4. Local Variable Usage Test

Copyright © 2009 Accenture All Rights Reserved. 30

Page 16: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

16IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Test Condition design for a DatabaseFor Database Operations , three cases should be tested in a unit test.

1 WHERE Clause1. WHERE Clause2. Data definition and Operation3. Stored procedure and function

Copyright © 2009 Accenture All Rights Reserved. 31

WHERE clause AND operations• WHERE clause :

– Test condition for AND operation :– The case of All conditions are TRUE– The case of All conditions are TRUE– The case of one condition is FALSE.

Example :

Copyright © 2009 Accenture All Rights Reserved. 32

Page 17: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

17IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

WHERE clause OR operations• WHERE clause :

– Test condition for OR operation :– The case of all FALSE conditions– The case of all FALSE conditions.– The case where one condition is TRUE

Example :

Copyright © 2009 Accenture All Rights Reserved. 33

WHERE clause IN operations• WHERE clause :

– IN operation existsEvery item needs to be covered in the IN operationsEvery item needs to be covered in the IN operations.

Example :

Copyright © 2009 Accenture All Rights Reserved. 34

Page 18: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

18IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Data Definition and Manipulation• Data Definition and Operation :

– UPDATE• The items are updated correctlyThe items are updated correctly• The impacted data scope is correct.• Check if expected number of rows are only updated.

– INSERT• The data is inserted successfully.• The default data and auto-generated data are inserted correctly.• Check if expected number of rows are inserted

Copyright © 2009 Accenture All Rights Reserved.

• Check if expected number of rows are inserted.

– DELETE• The deleted row is the target row to be deleted.• The related table and data are considered when executing the DELETE

operation.

35

Test condition design for GUI• Graphical User Interface :

– More and more complex data– Hundreds of GUI screens and thousands of GUI objects– Hundreds of GUI screens and thousands of GUI objects– Short of elaborated requirements– Difficult to judge if it is good or bad

GUI Testing :– Time consuming– Large amount of test conditions

Copyright © 2009 Accenture All Rights Reserved.

g– Easy to miss test points – Lots of underlying criteria

36

Page 19: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

19IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Test condition design for GUI

Copyright © 2009 Accenture All Rights Reserved. 37

Ensure the display meets the requirements

• Ensure the display of pages and elements are the same as those defined in the requirements.– Each page title is correctEach page title is correct– Each component ‘s color follows the design– The TAB follows the correct sequence left to right, top to bottom– The password input box is masked– The formatted data follows the pre-defined format– The number input box does not allow characters and/or ‘illegal’

number inputs.

Copyright © 2009 Accenture All Rights Reserved.

– For combined boxes that allow added data, the view is properlyarranged and the selection is enabled after the data is added.

– Drop-down List box allows selection– Depending on requirement make sure page refreshes completly or

only a part of it

38

Page 20: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

20IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Ensure the display meets the requirements

• Multiple-choice box allows multiple selections.• Date input boxes accept the correct date inputs, and rejects

incorrectly formatted inputincorrectly formatted input.• Single choice box allows only single selection: also check if the

single choice box allows you to leave the selection blank• The multi choice box allows multiple choices(including select

all)checks if it allows you to leave the choice blank• Check if the text box or some components gray out or behave

as intended in the design.

Copyright © 2009 Accenture All Rights Reserved.

g• After clicking the ‘Cancel’ button, check if the data in the

component is cleared or processed as specified.• After clicking the ‘Submit’ button, check if the data is submitted

or processed as specified.

39

Ensure the display meets the requirements

• Page Texts( Full shape, half shape, format, and spelling) are correct.

• The error log content and format are correct• The error log content and format are correct.• Check if the alignment follows the standard.• When the focus moves to the component, check which status it

should be, for example: ‘ select-all ,’ at the head’, ‘or ‘in the end’.• Check if there are any unrecognized characters.• Check if support for an accelerating key is needed.• Check if the page needs to support Resize and check if the

Copyright © 2009 Accenture All Rights Reserved.

• Check if the page needs to support Resize, and check if the page works properly after resize.

40

Page 21: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

21IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Ensure the functions are fully implemented.

• Ensure all of the validations or logic control functions for some elements are correctly and fully implemented.– Design test condition for the validations and logic control functionsDesign test condition for the validations and logic control functions

one-by-one.– May need to consider platform adaptability.

Copyright © 2009 Accenture All Rights Reserved. 41

Test Case Creation• Sample:

Sam is working as a developer in ABC project. He has analyzed therequirements and developed a login page of the application. He isq p g p g pprequired to create a test case for the login page.Following is the template that he uses to create the test case for the login page:

Test Case ID

Test Case Name

Description

Expected Result

Input Dat

Actual Result

Pass/Fail

Comments

Copyright © 2009 Accenture All Rights Reserved.

a

Page 22: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

22IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Sample Test Case

Test Case

Test Case

Description Expected Result

Input Data Actual Result

Pass/Fail

Comments

ID Name

TC 1.0 Login Type the following URL:https://portal.accenture.com

Login page is displayed

https://portal.accenture.com Login page was displayed

Pass

TC 2.0 Username

Check whether the username field is present

Username field is present

Username field was present

Pass

Copyright © 2009 Accenture All Rights Reserved.

TC 2.1 Username

Input the Valid data Username is displayed

[email protected] Username was displayed

Pass

Regression Test• The Regression test is when all the test conditions are re-

executed for function modules influenced by a change or bug fix to the program.o e p og a

• It is very important to ensure that the change or bug fix does not introduce any unwanted impact to the current system. During this test, the test data is the same as before so that it can be compared to the pre-defined expected test result. When there is a change during the software development life cycle, regression testing is needed. This test also happens during the Unit Test phase

Copyright © 2009 Accenture All Rights Reserved.

phase.

44

Page 23: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

23IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Performance Test• The Performance test checks the performance capability of

software or a system. Software cannot be considered ‘good’ when its performance is poor, even if it fulfills the required e s pe o a ce s poo , e e u s e equ edfunctionality. Therefore, it is necessary to conduct performance tests.

• To build a high performance software system, the performance should be considered at earlier phase, e.g., technical design phase. And the performance of every program unit is the basis of the whole software system. A f l P f T t h ld b d t d ft th

Copyright © 2009 Accenture All Rights Reserved.

• A formal Performance Test should be conducted after the Assembly Test. However, it is also sometimes necessary to check the performance during the Unit Test phase.

45

• Unit :Is the smallest testable part of an application.• Unit Testing :Validates the source code of a defined functional

unit

Key Points

unit.• Unit Test should cover :

– Code ,Branches, Paths, Cycles• Unit Testing Deliverables

– Test Conditions and Expected Results ,Test Cycle Control Sheet,Test Data, Test Drivers ,Test Environment Document

• A test condition is a statement that defines a constraint that

Copyright © 2009 Accenture All Rights Reserved. 46

• A test condition is a statement that defines a constraint that must be satisfied by the program being tested.

• Test Condition General Principles– Branch Coverage ,Condition Coverage,Loop Coverage, Interface

Coverage ,Logic Path Coverage

Page 24: Unit Testing PG

Programming Techniques Module 2: Fundamental Programming Concepts

2.1_FundamentalProgrammingConcepts.ppt

24IDCGRF001-ACopyright © 2009 Accenture All Rights Reserved.

Questions/ Comments• What questions or

comments do you have?

Copyright © 2009 Accenture All Rights Reserved. 47