SlideShare a Scribd company logo
Test Blueprints
Exposing Side Effects in Execution
Traces to Suport Writing Unit Tests



                  Adrian Lienhard, Tudor Gîrba,
                Orla Greevy and Oscar Nierstrasz
                          Software Composition Group
                         University of Bern, Switzerland


                 Thanks for the support:
Unit Testing

Everybody knows
  ...unit tests are important!
  ...write tests first!




CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Unit Testing

Everybody knows
  ...unit tests are important!
  ...write tests first!


Fair enough, but what if
  ...the code exists already?
  ...and you don’t understand it well?


CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Unit Testing

Yet, writing unit tests for
legacy systems is important.



“Tests are your life insurance”




   CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Challenges of Testing
         Legacy Code
   1. Selecting a unit to test
             choose unit of appropriate complexity

   2. Creating a fixture
             instantiate and set up graph of objects

   3. Executing the unit under test
             stimulate the fixture

   4. Verifying the expected behavior
             assert side effects and return value


CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Idea
Use example runs of the program to extract
test scenarios.


Perform a dynamic analysis to extract a test
blueprint that shows:
  - how to create a fixture for a selected unit
  - how to execute the unit
  - how to verify the expected behavior



  CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Program Instrumentation
    Example program execution                     Execution of existing tests
                             object flow +
                                                                          coverage data
                             execution trace data
                                        Analysis

                                             Tool




     Execution Trace                                  Test Blueprint

CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Test Blueprint

     Execution Trace                                             Test Blueprint

ASTChecker>>declareVariableNode:
  ...
  FunctionScope>>addTemp:


                                   Execution
      TempVar class>>new
         ...initialization...
                                     Unit
      TempVar>>name:
      TempVar>>scope:
      KeyedSet>>add:
          ...library code...

   ...




 CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Test Blueprint




                     Legend :Class existing instance
                                             existing reference

                                   :Class new instance
                                          new reference (side effect)




CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Creating the Fixture

Unit Test for addTemp: method
1   fscope := FunctionScope new.
2   varname := ’x’.




                                                                  Legend :Class existing instance
                                                                                          existing reference

                                                                                :Class new instance
                                                                                       new reference (side effect)




         CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Executing the Unit under Test

Unit Test for addTemp: method
1   fscope := FunctionScope new.
2   varname := ’x’.

3   var := fscope addTemp: name.




                                                                  Legend :Class existing instance
                                                                                          existing reference

                                                                                :Class new instance
                                                                                       new reference (side effect)




         CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Verifying Expected Behavior

Unit Test for addTemp: method
1   fscope := FunctionScope new.
2   varname := ’x’.

3   var := fscope addTemp: name.

4   self assert: var class = TempVar.
5   self assert: var name = varname.
6   self assert: var scope = fscope.
7   self assert: (                                                Legend :Class existing instance
      fscope tempVars includes: var).                                                     existing reference

                                                                                :Class new instance
                                                                                       new reference (side effect)




         CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Blueprint Examples
         IRBuilder>>add:                                             FunctionScope>>lookupVar:




                                                               NonClosureScopeFixer>>acceptVarNode:
      InstanceScope>>newMethodScope



ace                Test Blueprint




           CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Implementation
                                                    dynamic model               static model         Aliases are created
                                Instance        *                        1
                                                                                 Class
                                                                                                     when an object is:
                                                                                    1                ‣ instantiated
                                                   children
*                        1    activations
                                          0..1      *
                                                                         1
                                                                                    *                ‣ stored into or read
             Alias                          Activation *                        Method                 from a field
                         receiver       *
    0..1         *                     creator 1      root
    parent                                                                                           ‣ stored into or read
                     createdAliases                                                                    from an array
                                                      Execution-
                                                         Unit                                        ‣ passed as argument/
                                                                                                       return value
                      Object Flow meta-model

The flow of an object in the system is tracked by
the parent-child relationship of its Aliases

                      CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Implementation
                                      Execution Unit
                   w                         r                      r
object_1
                                    w
object_2
                    w                                 r        w
object_3

               Imported                                                  Exported
              references                                                references
                                                                                         t

Imported references = used state (->fixture)
Exported references = side effects (->assertions)



  CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Initial Case Study I
                                       How straightforward is it to use the
                                       proposed tool?

                                       Target system: Insurance broker
                                       application (520 classes, 6 years old)

  Observations
  - One developer produced 12 tests in 2h
  - Average fixture size: 5
  - Average side effects: 4
    - Test Blueprint worked surprisingly well
            ...yet, sporadically resorted to consulting source code
    - Insufficient support for selecting execution units
CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Initial Case Study II
How do tests written using our approach differ compared
to conventional tests written by an expert?

Content Management System (377 classes)

Study: selected 14 unit tests, removed all 84 assertions
and then systematically rewrote them using our tool

Resulting assertions
- 72 identical
- 5 additional
- 12 missed ==> verification that special objects are left unmodified
                             our approach does not detect unmodified objects
                             that are worthwhile to verify!

      CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Conclusions
Yes, we can write unit test for not well known code
  ...by exploiting runtime information

Test Blueprints tell us
  ...how the fixture and assertions have to look like

Difficulties are how to
  ...select good execution units in the trace
  ...initialize objects to bring them into the desired state




     CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
Conclusions
Yes, we can write unit test for not well known code
  ...by exploiting runtime information

Test Blueprints tell us
  ...how the fixture and assertions have to look like

Difficulties are how to
  ...select good execution units in the trace
  ...initialize objects to bring them into the desired state


                                     Questions?
     CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch

More Related Content

PPTX
Pi j4.2 software-reliability
PDF
A Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
PDF
JDK Power Tools
PPT
Standard exceptions
DOCX
Unit5 java
PPTX
Mock your way with Mockito
PDF
PVS-Studio Meets Octave
PPTX
Introduction to Software Testing
Pi j4.2 software-reliability
A Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
JDK Power Tools
Standard exceptions
Unit5 java
Mock your way with Mockito
PVS-Studio Meets Octave
Introduction to Software Testing

What's hot (18)

PDF
PPTX
Android Unit Test
PDF
Some testing - Everything you should know about testing to go with @pedro_g_s...
PDF
#codemotion2016: Everything you should know about testing to go with @pedro_g...
PPTX
PDF
Server1
PPTX
Qunit Java script Un
PPTX
Mocking with Mockito
PPT
Comp102 lec 10
PDF
Stop Making Excuses and Start Testing Your JavaScript
PPTX
The operation principles of PVS-Studio static code analyzer
PDF
Object Oriented Exploitation: New techniques in Windows mitigation bypass
PPTX
J unit스터디슬라이드
PPTX
Junit, mockito, etc
PDF
Botsing demo
PDF
All about unit testing using (power) mock
PDF
Checking VirtualDub
ODP
Grails unit testing
Android Unit Test
Some testing - Everything you should know about testing to go with @pedro_g_s...
#codemotion2016: Everything you should know about testing to go with @pedro_g...
Server1
Qunit Java script Un
Mocking with Mockito
Comp102 lec 10
Stop Making Excuses and Start Testing Your JavaScript
The operation principles of PVS-Studio static code analyzer
Object Oriented Exploitation: New techniques in Windows mitigation bypass
J unit스터디슬라이드
Junit, mockito, etc
Botsing demo
All about unit testing using (power) mock
Checking VirtualDub
Grails unit testing
Ad

Viewers also liked (20)

PDF
Blueprint of exam questions
PPTX
Blueprint in education
PPT
Blueprint ppt
PPT
How to make question paper
PDF
Class 10 Cbse Social Science Sample Paper Model 1
PDF
Cbse blue print maths
PPT
Assessment (Blueprint)
PDF
+2 design of the question paper and blue print
PDF
The Assessment Blueprint
PPT
Planning an achievement test and assessment
PPTX
Achievement tests
PPT
Question Paper Setting
PPTX
achievement test
PPT
Table of specifications 2013 copy
PPT
Basic blueprint reading
DOCX
HGMBJH,J,,N,NMNMFCDF
PPT
Walker SAM Presentation
PPTX
Paper setting principles and practices
PDF
Mathematics 2014 sample paper and blue print
DOC
Kisi kisi uas basa jawa
Blueprint of exam questions
Blueprint in education
Blueprint ppt
How to make question paper
Class 10 Cbse Social Science Sample Paper Model 1
Cbse blue print maths
Assessment (Blueprint)
+2 design of the question paper and blue print
The Assessment Blueprint
Planning an achievement test and assessment
Achievement tests
Question Paper Setting
achievement test
Table of specifications 2013 copy
Basic blueprint reading
HGMBJH,J,,N,NMNMFCDF
Walker SAM Presentation
Paper setting principles and practices
Mathematics 2014 sample paper and blue print
Kisi kisi uas basa jawa
Ad

Similar to Test Blueprints (20)

PPTX
saihw1_weka_tutorial.pptx - Machine Discovery and Social Network ...
PDF
Object Flow Analysis
PDF
Dynamic Object Flow Analysis (PhD Defense)
PDF
How to Test Asynchronous Code (v2)
PPTX
Object detection
PPTX
PPTX
Java Unit Test and Coverage Introduction
PDF
2011-02-03 LA RubyConf Rails3 TDD Workshop
PDF
TRunner
PPS
06 iec t1_s1_oo_ps_session_08
PDF
Gallio Crafting A Toolchain
PPTX
Javascript: master this
PPTX
EclipseMAT
PPTX
Static code analysis: what? how? why?
PDF
Unit test-using-spock in Grails
PPTX
unit test in node js - test cases in node
PDF
TDD: Develop, Refactor and Release With Confidence
PDF
Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016
PDF
Attack-driven defense
PDF
Implementing Data Visualization Apps on iOS Devices
saihw1_weka_tutorial.pptx - Machine Discovery and Social Network ...
Object Flow Analysis
Dynamic Object Flow Analysis (PhD Defense)
How to Test Asynchronous Code (v2)
Object detection
Java Unit Test and Coverage Introduction
2011-02-03 LA RubyConf Rails3 TDD Workshop
TRunner
06 iec t1_s1_oo_ps_session_08
Gallio Crafting A Toolchain
Javascript: master this
EclipseMAT
Static code analysis: what? how? why?
Unit test-using-spock in Grails
unit test in node js - test cases in node
TDD: Develop, Refactor and Release With Confidence
Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016
Attack-driven defense
Implementing Data Visualization Apps on iOS Devices

More from lienhard (10)

PDF
Chicken
PDF
Prototype-based Programming with JavaScript
PDF
Virtual Machines Lecture
PDF
Flow-Centric, Back-In-Time Debugging
PDF
OORPT Dynamic Analysis
PDF
Rapid Prototyping Of Visualizations Using Mondrian
PDF
Tracking Objects To Detect Feature Dependencies
PDF
Practical Object-Oriented Back-in-Time Debugging
PDF
Object Flow Analysis
PDF
Identifying Traits with Formal Concept Analysis
Chicken
Prototype-based Programming with JavaScript
Virtual Machines Lecture
Flow-Centric, Back-In-Time Debugging
OORPT Dynamic Analysis
Rapid Prototyping Of Visualizations Using Mondrian
Tracking Objects To Detect Feature Dependencies
Practical Object-Oriented Back-in-Time Debugging
Object Flow Analysis
Identifying Traits with Formal Concept Analysis

Recently uploaded (20)

PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Web App vs Mobile App What Should You Build First.pdf
PPTX
1. Introduction to Computer Programming.pptx
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
project resource management chapter-09.pdf
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Encapsulation theory and applications.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Hindi spoken digit analysis for native and non-native speakers
PDF
August Patch Tuesday
Accuracy of neural networks in brain wave diagnosis of schizophrenia
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Getting Started with Data Integration: FME Form 101
OMC Textile Division Presentation 2021.pptx
Web App vs Mobile App What Should You Build First.pdf
1. Introduction to Computer Programming.pptx
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Univ-Connecticut-ChatGPT-Presentaion.pdf
Hybrid model detection and classification of lung cancer
project resource management chapter-09.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Enhancing emotion recognition model for a student engagement use case through...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Encapsulation theory and applications.pdf
cloud_computing_Infrastucture_as_cloud_p
Agricultural_Statistics_at_a_Glance_2022_0.pdf
TLE Review Electricity (Electricity).pptx
Hindi spoken digit analysis for native and non-native speakers
August Patch Tuesday

Test Blueprints

  • 1. Test Blueprints Exposing Side Effects in Execution Traces to Suport Writing Unit Tests Adrian Lienhard, Tudor Gîrba, Orla Greevy and Oscar Nierstrasz Software Composition Group University of Bern, Switzerland Thanks for the support:
  • 2. Unit Testing Everybody knows ...unit tests are important! ...write tests first! CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 3. Unit Testing Everybody knows ...unit tests are important! ...write tests first! Fair enough, but what if ...the code exists already? ...and you don’t understand it well? CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 4. Unit Testing Yet, writing unit tests for legacy systems is important. “Tests are your life insurance” CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 5. Challenges of Testing Legacy Code 1. Selecting a unit to test choose unit of appropriate complexity 2. Creating a fixture instantiate and set up graph of objects 3. Executing the unit under test stimulate the fixture 4. Verifying the expected behavior assert side effects and return value CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 6. Idea Use example runs of the program to extract test scenarios. Perform a dynamic analysis to extract a test blueprint that shows: - how to create a fixture for a selected unit - how to execute the unit - how to verify the expected behavior CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 7. Program Instrumentation Example program execution Execution of existing tests object flow + coverage data execution trace data Analysis Tool Execution Trace Test Blueprint CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 8. Test Blueprint Execution Trace Test Blueprint ASTChecker>>declareVariableNode: ... FunctionScope>>addTemp: Execution TempVar class>>new ...initialization... Unit TempVar>>name: TempVar>>scope: KeyedSet>>add: ...library code... ... CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 9. Test Blueprint Legend :Class existing instance existing reference :Class new instance new reference (side effect) CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 10. Creating the Fixture Unit Test for addTemp: method 1 fscope := FunctionScope new. 2 varname := ’x’. Legend :Class existing instance existing reference :Class new instance new reference (side effect) CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 11. Executing the Unit under Test Unit Test for addTemp: method 1 fscope := FunctionScope new. 2 varname := ’x’. 3 var := fscope addTemp: name. Legend :Class existing instance existing reference :Class new instance new reference (side effect) CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 12. Verifying Expected Behavior Unit Test for addTemp: method 1 fscope := FunctionScope new. 2 varname := ’x’. 3 var := fscope addTemp: name. 4 self assert: var class = TempVar. 5 self assert: var name = varname. 6 self assert: var scope = fscope. 7 self assert: ( Legend :Class existing instance fscope tempVars includes: var). existing reference :Class new instance new reference (side effect) CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 13. Blueprint Examples IRBuilder>>add: FunctionScope>>lookupVar: NonClosureScopeFixer>>acceptVarNode: InstanceScope>>newMethodScope ace Test Blueprint CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 14. Implementation dynamic model static model Aliases are created Instance * 1 Class when an object is: 1 ‣ instantiated children * 1 activations 0..1 * 1 * ‣ stored into or read Alias Activation * Method from a field receiver * 0..1 * creator 1 root parent ‣ stored into or read createdAliases from an array Execution- Unit ‣ passed as argument/ return value Object Flow meta-model The flow of an object in the system is tracked by the parent-child relationship of its Aliases CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 15. Implementation Execution Unit w r r object_1 w object_2 w r w object_3 Imported Exported references references t Imported references = used state (->fixture) Exported references = side effects (->assertions) CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 16. Initial Case Study I How straightforward is it to use the proposed tool? Target system: Insurance broker application (520 classes, 6 years old) Observations - One developer produced 12 tests in 2h - Average fixture size: 5 - Average side effects: 4 - Test Blueprint worked surprisingly well ...yet, sporadically resorted to consulting source code - Insufficient support for selecting execution units CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 17. Initial Case Study II How do tests written using our approach differ compared to conventional tests written by an expert? Content Management System (377 classes) Study: selected 14 unit tests, removed all 84 assertions and then systematically rewrote them using our tool Resulting assertions - 72 identical - 5 additional - 12 missed ==> verification that special objects are left unmodified our approach does not detect unmodified objects that are worthwhile to verify! CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 18. Conclusions Yes, we can write unit test for not well known code ...by exploiting runtime information Test Blueprints tell us ...how the fixture and assertions have to look like Difficulties are how to ...select good execution units in the trace ...initialize objects to bring them into the desired state CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch
  • 19. Conclusions Yes, we can write unit test for not well known code ...by exploiting runtime information Test Blueprints tell us ...how the fixture and assertions have to look like Difficulties are how to ...select good execution units in the trace ...initialize objects to bring them into the desired state Questions? CSMR’08 : : Test Blueprints : : Adrian Lienhard : : www.adrian-lienhard.ch : : lienhard@iam.unibe.ch