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

Unit Testing Standards - Recommended Best Practices
Unit Testing Standards - Recommended Best PracticesUnit Testing Standards - Recommended Best Practices
Unit Testing Standards - Recommended Best Practices
Vitaliy Kulikov
 
NetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportNetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience Report
Anton Arhipov
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3
Oliver Klee
 
Test-Driven Development for TYPO3
Test-Driven Development for TYPO3Test-Driven Development for TYPO3
Test-Driven Development for TYPO3
Oliver Klee
 
Test-Driven Development for TYPO3 @ T3CON12DE
Test-Driven Development for TYPO3 @ T3CON12DETest-Driven Development for TYPO3 @ T3CON12DE
Test-Driven Development for TYPO3 @ T3CON12DE
Oliver Klee
 

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

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
Mindy P
 
Mis Gustos
Mis GustosMis Gustos
Mis Gustos
jexx
 
D論公聴会資料
D論公聴会資料D論公聴会資料
D論公聴会資料
fenrir-naru
 

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

J unit스터디슬라이드
J unit스터디슬라이드J unit스터디슬라이드
J unit스터디슬라이드
ksain
 
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 example
Guo Albert
 

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

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
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
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 

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