SlideShare a Scribd company logo
1 of 47
Refactoring
     Test Code

Eduardo Guerra
Refactoring
Cleaning the source code to
improve its design without
changing its external
behavior.
Source code is simple to
understand and can
address only the current
requirements.


                     It is easier to maintain and
                     evolve a simple and
                     organized code structure.
But, does
   test code
 need to be
refactored?
To add or change
functionality you need to
 deal with existing tests!
“.. keep it (tests) at good object-
oriented design, so than if we need
to make a change or add something
or kick something away , we only
have to do that in may be one place
and not in two hundred different
scripts.“
                            Lisa Crispin
                     Author of “Agile Testing“
          Software Engineering Radio Podcast
How can I
  How can I
                     verify
define what is
                 maintainance
the test code
                 of test code
  behavior?
                  behavior?
Undestanding
       Tests
Test Target → instance used
A   in the test.

    Action → changes environment
    or the Test Target.

    Assertion → comparison
    between the Test Target behavior
    and the expected one.
Test → a sequence
of at least one action
   and one assertion.

          A      A
A      A
A                              A
        A     A      A
Test Suite → a set of tests that
can be independently executed.
A     A
 A                            A
           A     A   A
Initialization       Finalization
Verification → a sequence
    of actions and one assertion
    dealing with the same target.
      A       A   A   A


A         A       A   A     A

    V1                V2
Two test suites can be
considered equivalent
when they perform the
 same verifications!
A   A
 A                            A
         A   A    A   A
Equivalent! Same Verifications!

     A   A    A
     A   A    A           A
     A   A    A   A
Test Refactoring
   Classification
Test Method

   Test Class
     Test Suite
Test Method
      Refactorings
Changes actions and
  assertions for
 equivalent ones.
Test Method
              Refactorings
●   Add Assertion Explanation
●   Introduce Assertion Method
●   Simplify Test Scenario
●   Separate Action from Assertion
●   Decompose Assertion
Decompose
                               Assertion
@Test                              @Test
public void needyEmployee{         public void needyEmployee{
  Employee e = new Employee();       Employee e = new Employee();
  e.setPayment(300);                 e.setPayment(300);
  assertTrue(“Employee Profile”,     assertTrue(“Needy employee”,
      e.isNeedy() &&                     e.isNeedy());
      e.getDiscount() == 0);         assertEquals(“Employee is free”,
}                                       e.getDiscount(), 0);
                                   }
Test Class
        Refactorings
Reorganize actions
  and assertions
among methods in
the same test class.
Test Class
                Refactorings
●   Add Fixture
●   Introduce Initialization Method
●   Inline Initialization Method
●   Join Incremental Tests
●   Split Test
●   Join Similar Tests with
     Distinct Data
Join Incremental
@Test public void oneItem(){
  cart.addItem(100);
  assertEquals(115,cart.total());
}


                                            Tests
@Test public void twoItens(){
  cart.addItem(100);
  cart.addItem(100);
  assertEquals(200, cart.total());
}
@Test public void threeItens(){
  cart.addItem(100);
  cart.addItem(100);
  cart.addItem(100);
  assertEquals(385, cart.total());
}
                                     @Test public void insertItens(){
                                       cart.addItem(100);
                                       assertEquals(115,cart.total());
                                       cart.addItem(100);
                                       assertEquals(200, cart.total());
                                       cart.addItem(100);
                                       assertEquals(385, cart.total());
                                     }
Test Suite
       Refactorings
 Reorganize test
methods among the
   test classes.
Test Suite
              Refactorings
●   Mirror Hierarchy to Tests
●   Pull Up Test
●   Pull Down Test
●   Create Template Test
●   Split Tests from Composition
<<abstract>>
                                                        SuperClass


                                                       +methodSuper()
                                                                                    Mirror Hierarchy
                      TestSubClass2
                                                                        SubClass2
                                                                                            to Tests
                     +testMethodSuper()
                                                                       +methodSub2()
                     +testMethodSub2()

 TestSubClass1                             SubClass1


+testMethodSuper()                        +methodSub1()
+testMethodSub1()

                                                                                         <<abstract>>                                      <<abstract>>
                                                                                       TestSuperClass                                      SuperClass


                                                                                                                                          +methodSuper()




                                                                                                         TestSubClass2
                                                                                                                                                           SubClass2


                                                                                                        +testMethodSuper()
                                                                                                                                                          +methodSub2()
                                                                                                        +testMethodSub2()

                                                                             TestSubClass1
                                                                                                                              SubClass1


                                                                           +testMethodSuper()                                +methodSub1()
                                                                           +testMethodSub1()
Why test
smells appear?
Wrong
step size
Many
 increments in
the same test
Test code design evolves,
 like the production code
       design evolves!
Refactoring           Test bar should
Production Code         continue green

                    But some bad
                  smells can appear
                   in the test code!
ROUND 1
They have something in common...


                        Extract
                       Superclass

                        Pull Up
                        Method
There are some
duplicated tests
     there!
Refactoring the tests!
                   Mirror
                 Hierarchy
                  to Tests

                   Add
                  Fixture

                 Pull Up
                  Test
ROUND 2
ScheduledExecution duplicate
 some Action functionality.


                   Move Logic
                   to Delegate
The test is now covering one
     possible composition of
 ScheduledExecution, but it can be
composed by any Executor subclass!



    Split Tests
      From
   Composition
Refactoring
 Knockout
Refactorings that
change the classes
structure usually
create a test bad smell!
Future Vision
 of Test Code
  Refactoring
Automate
Test Refactorings
Creation of an Eclipse plugin to
automate test refactorings and
bad smell detection
independently from the test
framework.
Verify Test Behavior
Maintainance with Mutants
        Create a tool that executes
               tests on test target
          mutants before and after
          the refactoring to verify if
              the test behavior was
                            changed.
“Unlike production code, which you
don't have a choice but to maintain,
tests are optional, you can always
stop running them and just throw
them away, and any investment
that you made (...) are just throw
out the window.“
                   Gerard Meszaros
              Author of “xUnit Test Patterns“
          Software Engineering Radio Podcast

More Related Content

What's hot

xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database TestingChris Oldwood
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4jeresig
 
Unit Testing Standards - Recommended Best Practices
Unit Testing Standards - Recommended Best PracticesUnit Testing Standards - Recommended Best Practices
Unit Testing Standards - Recommended Best PracticesVitaliy Kulikov
 
NetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportNetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportAnton Arhipov
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest MatchersShai Yallin
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3Oliver Klee
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingBethmi Gunasekara
 
Example First / A Sane Test-Driven Approach to Programming
Example First / A Sane Test-Driven Approach to ProgrammingExample First / A Sane Test-Driven Approach to Programming
Example First / A Sane Test-Driven Approach to ProgrammingJonathan Acker
 
Test-Driven Development for TYPO3
Test-Driven Development for TYPO3Test-Driven Development for TYPO3
Test-Driven Development for TYPO3Oliver Klee
 
Dont do it in android test automation
Dont do it in android test automationDont do it in android test automation
Dont do it in android test automationALEKSEITIURIN
 
Test-Driven Development for TYPO3 @ T3CON12DE
Test-Driven Development for TYPO3 @ T3CON12DETest-Driven Development for TYPO3 @ T3CON12DE
Test-Driven Development for TYPO3 @ T3CON12DEOliver Klee
 
Runtime Tools
Runtime ToolsRuntime Tools
Runtime ToolsESUG
 

What's hot (20)

xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database Testing
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
 
Unit Testing Standards - Recommended Best Practices
Unit Testing Standards - Recommended Best PracticesUnit Testing Standards - Recommended Best Practices
Unit Testing Standards - Recommended Best Practices
 
testng
testngtestng
testng
 
NetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportNetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience Report
 
Writing and using Hamcrest Matchers
Writing and using Hamcrest MatchersWriting and using Hamcrest Matchers
Writing and using Hamcrest Matchers
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
TestNG vs. JUnit4
TestNG vs. JUnit4TestNG vs. JUnit4
TestNG vs. JUnit4
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3
 
Testing in-groovy
Testing in-groovyTesting in-groovy
Testing in-groovy
 
TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
 
Example First / A Sane Test-Driven Approach to Programming
Example First / A Sane Test-Driven Approach to ProgrammingExample First / A Sane Test-Driven Approach to Programming
Example First / A Sane Test-Driven Approach to Programming
 
Test-Driven Development for TYPO3
Test-Driven Development for TYPO3Test-Driven Development for TYPO3
Test-Driven Development for TYPO3
 
Dont do it in android test automation
Dont do it in android test automationDont do it in android test automation
Dont do it in android test automation
 
Java custom annotations example
Java custom annotations exampleJava custom annotations example
Java custom annotations example
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
Test-Driven Development for TYPO3 @ T3CON12DE
Test-Driven Development for TYPO3 @ T3CON12DETest-Driven Development for TYPO3 @ T3CON12DE
Test-Driven Development for TYPO3 @ T3CON12DE
 
Runtime Tools
Runtime ToolsRuntime Tools
Runtime Tools
 
Test ng
Test ngTest ng
Test ng
 

Viewers also liked

Wie wird mein Code testbar?
Wie wird mein Code testbar?Wie wird mein Code testbar?
Wie wird mein Code testbar?David Völkel
 
Refactoring tested code - has mocking gone wrong?
Refactoring tested code - has mocking gone wrong?Refactoring tested code - has mocking gone wrong?
Refactoring tested code - has mocking gone wrong?Ben Stopford
 
51 basic shapes and formulas
51 basic shapes and formulas51 basic shapes and formulas
51 basic shapes and formulasalg1testreview
 
Barniz Arrugado | Barniz Efecto Óxido
Barniz Arrugado | Barniz Efecto ÓxidoBarniz Arrugado | Barniz Efecto Óxido
Barniz Arrugado | Barniz Efecto ÓxidoBarnize Special
 
Brief portfolio as a Marketing executive, Walt Disney studios
Brief portfolio as a Marketing executive, Walt Disney studiosBrief portfolio as a Marketing executive, Walt Disney studios
Brief portfolio as a Marketing executive, Walt Disney studiosMindy P
 
Mis Gustos
Mis GustosMis Gustos
Mis Gustosjexx
 
Refactoring bad codesmell
Refactoring bad codesmellRefactoring bad codesmell
Refactoring bad codesmellhyunglak kim
 
Introduction to AV Foundation
Introduction to AV FoundationIntroduction to AV Foundation
Introduction to AV FoundationChris Adamson
 
Wann soll ich mocken?
Wann soll ich mocken?Wann soll ich mocken?
Wann soll ich mocken?David Völkel
 
Firebase: Totally Not Parse All Over Again (Unless It Is)
Firebase: Totally Not Parse All Over Again (Unless It Is)Firebase: Totally Not Parse All Over Again (Unless It Is)
Firebase: Totally Not Parse All Over Again (Unless It Is)Chris Adamson
 
D論公聴会資料
D論公聴会資料D論公聴会資料
D論公聴会資料fenrir-naru
 
Mockist vs Classicists TDD
Mockist vs Classicists TDDMockist vs Classicists TDD
Mockist vs Classicists TDDDavid Völkel
 

Viewers also liked (20)

Wie wird mein Code testbar?
Wie wird mein Code testbar?Wie wird mein Code testbar?
Wie wird mein Code testbar?
 
Refactoring tested code - has mocking gone wrong?
Refactoring tested code - has mocking gone wrong?Refactoring tested code - has mocking gone wrong?
Refactoring tested code - has mocking gone wrong?
 
Catalog 1
Catalog 1Catalog 1
Catalog 1
 
AFETO 2
AFETO 2AFETO 2
AFETO 2
 
51 basic shapes and formulas
51 basic shapes and formulas51 basic shapes and formulas
51 basic shapes and formulas
 
Barniz Arrugado | Barniz Efecto Óxido
Barniz Arrugado | Barniz Efecto ÓxidoBarniz Arrugado | Barniz Efecto Óxido
Barniz Arrugado | Barniz Efecto Óxido
 
Brief portfolio as a Marketing executive, Walt Disney studios
Brief portfolio as a Marketing executive, Walt Disney studiosBrief portfolio as a Marketing executive, Walt Disney studios
Brief portfolio as a Marketing executive, Walt Disney studios
 
COMPOSITIONS
COMPOSITIONSCOMPOSITIONS
COMPOSITIONS
 
Mis Gustos
Mis GustosMis Gustos
Mis Gustos
 
Graffins Cert
Graffins CertGraffins Cert
Graffins Cert
 
Mailers
MailersMailers
Mailers
 
Refactoring bad codesmell
Refactoring bad codesmellRefactoring bad codesmell
Refactoring bad codesmell
 
Introduction to AV Foundation
Introduction to AV FoundationIntroduction to AV Foundation
Introduction to AV Foundation
 
CV_Venkatesh_Vaikunthe
CV_Venkatesh_VaikuntheCV_Venkatesh_Vaikunthe
CV_Venkatesh_Vaikunthe
 
Wann soll ich mocken?
Wann soll ich mocken?Wann soll ich mocken?
Wann soll ich mocken?
 
Firebase: Totally Not Parse All Over Again (Unless It Is)
Firebase: Totally Not Parse All Over Again (Unless It Is)Firebase: Totally Not Parse All Over Again (Unless It Is)
Firebase: Totally Not Parse All Over Again (Unless It Is)
 
Round Timeline Photos Diagram
Round Timeline Photos DiagramRound Timeline Photos Diagram
Round Timeline Photos Diagram
 
D論公聴会資料
D論公聴会資料D論公聴会資料
D論公聴会資料
 
Karyotyping
KaryotypingKaryotyping
Karyotyping
 
Mockist vs Classicists TDD
Mockist vs Classicists TDDMockist vs Classicists TDD
Mockist vs Classicists TDD
 

Similar to Refactoring test code

Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnitAktuğ Urun
 
Python: Object-Oriented Testing (Unit Testing)
Python: Object-Oriented Testing (Unit Testing)Python: Object-Oriented Testing (Unit Testing)
Python: Object-Oriented Testing (Unit Testing)Damian T. Gordon
 
Dynamic Analysis - SCOTCH: Improving Test-to-Code Traceability using Slicing ...
Dynamic Analysis - SCOTCH: Improving Test-to-Code Traceability using Slicing ...Dynamic Analysis - SCOTCH: Improving Test-to-Code Traceability using Slicing ...
Dynamic Analysis - SCOTCH: Improving Test-to-Code Traceability using Slicing ...ICSM 2011
 
J unit스터디슬라이드
J unit스터디슬라이드J unit스터디슬라이드
J unit스터디슬라이드ksain
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMEREVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMERAndrey Karpov
 
Testing orm based code
Testing orm based codeTesting orm based code
Testing orm based codeViktor Turskyi
 
Just in Time Resourcing
Just in Time ResourcingJust in Time Resourcing
Just in Time ResourcingESUG
 
Test-driven development for TYPO3 (T3DD11)
Test-driven development for TYPO3 (T3DD11)Test-driven development for TYPO3 (T3DD11)
Test-driven development for TYPO3 (T3DD11)Oliver Klee
 
Template method pattern example
Template method pattern exampleTemplate method pattern example
Template method pattern exampleGuo Albert
 
Be smart when testing your Akka code
Be smart when testing your Akka codeBe smart when testing your Akka code
Be smart when testing your Akka codeMykhailo Kotsur
 

Similar to Refactoring test code (20)

Power mock
Power mockPower mock
Power mock
 
Unit testing
Unit testingUnit testing
Unit testing
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Python: Object-Oriented Testing (Unit Testing)
Python: Object-Oriented Testing (Unit Testing)Python: Object-Oriented Testing (Unit Testing)
Python: Object-Oriented Testing (Unit Testing)
 
Unit testing
Unit testingUnit testing
Unit testing
 
Junit
JunitJunit
Junit
 
Dynamic Analysis - SCOTCH: Improving Test-to-Code Traceability using Slicing ...
Dynamic Analysis - SCOTCH: Improving Test-to-Code Traceability using Slicing ...Dynamic Analysis - SCOTCH: Improving Test-to-Code Traceability using Slicing ...
Dynamic Analysis - SCOTCH: Improving Test-to-Code Traceability using Slicing ...
 
J unit스터디슬라이드
J unit스터디슬라이드J unit스터디슬라이드
J unit스터디슬라이드
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
Scala test
Scala testScala test
Scala test
 
Scala test
Scala testScala test
Scala test
 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMEREVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
 
Testing orm based code
Testing orm based codeTesting orm based code
Testing orm based code
 
Just in Time Resourcing
Just in Time ResourcingJust in Time Resourcing
Just in Time Resourcing
 
Test-driven development for TYPO3 (T3DD11)
Test-driven development for TYPO3 (T3DD11)Test-driven development for TYPO3 (T3DD11)
Test-driven development for TYPO3 (T3DD11)
 
Template method pattern example
Template method pattern exampleTemplate method pattern example
Template method pattern example
 
Be smart when testing your Akka code
Be smart when testing your Akka codeBe smart when testing your Akka code
Be smart when testing your Akka code
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Refactoring test code

  • 1. Refactoring Test Code Eduardo Guerra
  • 2. Refactoring Cleaning the source code to improve its design without changing its external behavior.
  • 3. Source code is simple to understand and can address only the current requirements. It is easier to maintain and evolve a simple and organized code structure.
  • 4. But, does test code need to be refactored?
  • 5. To add or change functionality you need to deal with existing tests!
  • 6. “.. keep it (tests) at good object- oriented design, so than if we need to make a change or add something or kick something away , we only have to do that in may be one place and not in two hundred different scripts.“ Lisa Crispin Author of “Agile Testing“ Software Engineering Radio Podcast
  • 7. How can I How can I verify define what is maintainance the test code of test code behavior? behavior?
  • 8. Undestanding Tests
  • 9. Test Target → instance used A in the test. Action → changes environment or the Test Target. Assertion → comparison between the Test Target behavior and the expected one.
  • 10. Test → a sequence of at least one action and one assertion. A A
  • 11. A A A A A A A Test Suite → a set of tests that can be independently executed.
  • 12. A A A A A A A Initialization Finalization
  • 13. Verification → a sequence of actions and one assertion dealing with the same target. A A A A A A A A A V1 V2
  • 14. Two test suites can be considered equivalent when they perform the same verifications!
  • 15. A A A A A A A A Equivalent! Same Verifications! A A A A A A A A A A A
  • 16. Test Refactoring Classification
  • 17. Test Method Test Class Test Suite
  • 18. Test Method Refactorings Changes actions and assertions for equivalent ones.
  • 19. Test Method Refactorings ● Add Assertion Explanation ● Introduce Assertion Method ● Simplify Test Scenario ● Separate Action from Assertion ● Decompose Assertion
  • 20. Decompose Assertion @Test @Test public void needyEmployee{ public void needyEmployee{ Employee e = new Employee(); Employee e = new Employee(); e.setPayment(300); e.setPayment(300); assertTrue(“Employee Profile”, assertTrue(“Needy employee”, e.isNeedy() && e.isNeedy()); e.getDiscount() == 0); assertEquals(“Employee is free”, } e.getDiscount(), 0); }
  • 21. Test Class Refactorings Reorganize actions and assertions among methods in the same test class.
  • 22. Test Class Refactorings ● Add Fixture ● Introduce Initialization Method ● Inline Initialization Method ● Join Incremental Tests ● Split Test ● Join Similar Tests with Distinct Data
  • 23. Join Incremental @Test public void oneItem(){ cart.addItem(100); assertEquals(115,cart.total()); } Tests @Test public void twoItens(){ cart.addItem(100); cart.addItem(100); assertEquals(200, cart.total()); } @Test public void threeItens(){ cart.addItem(100); cart.addItem(100); cart.addItem(100); assertEquals(385, cart.total()); } @Test public void insertItens(){ cart.addItem(100); assertEquals(115,cart.total()); cart.addItem(100); assertEquals(200, cart.total()); cart.addItem(100); assertEquals(385, cart.total()); }
  • 24. Test Suite Refactorings Reorganize test methods among the test classes.
  • 25. Test Suite Refactorings ● Mirror Hierarchy to Tests ● Pull Up Test ● Pull Down Test ● Create Template Test ● Split Tests from Composition
  • 26. <<abstract>> SuperClass +methodSuper() Mirror Hierarchy TestSubClass2 SubClass2 to Tests +testMethodSuper() +methodSub2() +testMethodSub2() TestSubClass1 SubClass1 +testMethodSuper() +methodSub1() +testMethodSub1() <<abstract>> <<abstract>> TestSuperClass SuperClass +methodSuper() TestSubClass2 SubClass2 +testMethodSuper() +methodSub2() +testMethodSub2() TestSubClass1 SubClass1 +testMethodSuper() +methodSub1() +testMethodSub1()
  • 30. Test code design evolves, like the production code design evolves!
  • 31. Refactoring Test bar should Production Code continue green But some bad smells can appear in the test code!
  • 32.
  • 34. They have something in common... Extract Superclass Pull Up Method
  • 36. Refactoring the tests! Mirror Hierarchy to Tests Add Fixture Pull Up Test
  • 38. ScheduledExecution duplicate some Action functionality. Move Logic to Delegate
  • 39.
  • 40. The test is now covering one possible composition of ScheduledExecution, but it can be composed by any Executor subclass! Split Tests From Composition
  • 41.
  • 43. Refactorings that change the classes structure usually create a test bad smell!
  • 44. Future Vision of Test Code Refactoring
  • 45. Automate Test Refactorings Creation of an Eclipse plugin to automate test refactorings and bad smell detection independently from the test framework.
  • 46. Verify Test Behavior Maintainance with Mutants Create a tool that executes tests on test target mutants before and after the refactoring to verify if the test behavior was changed.
  • 47. “Unlike production code, which you don't have a choice but to maintain, tests are optional, you can always stop running them and just throw them away, and any investment that you made (...) are just throw out the window.“ Gerard Meszaros Author of “xUnit Test Patterns“ Software Engineering Radio Podcast