automock: interaction-based mock code generation

29
Sabrina de F. Souto, UFCG, Brazil [email protected] Supervisor: Dalton Serey, UFCG, Brazil [email protected] Student Forum

Upload: sabrina-souto

Post on 13-May-2015

102 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Automock: Interaction-Based Mock Code Generation

Sabrina  de  F.  Souto,  UFCG,  Brazil  [email protected]  

 Supervisor:  Dalton  Serey,  UFCG,  Brazil  

[email protected]  

Student  Forum  

Page 2: Automock: Interaction-Based Mock Code Generation

¡  Introduction  §  Unit  Test  

§  Mock  Objects  

¡  The  Problem  

¡  Solution  

¡  Evaluation  2

Page 3: Automock: Interaction-Based Mock Code Generation

¡  A  unit  is  the  smallest  testable  part  of  an  application    

¡  In  the  object  oriented  paradigm  it  is  a  class  

¡  A  unit   test   consists   of   a   fixed   sequence  of  method  invocations   with   arguments   that   explores   a  particular  aspect  of   the  behavior  of   the  class  under  test  –  CUT  

3

Page 4: Automock: Interaction-Based Mock Code Generation

¡  Testing   a   unit   in   isolation   is   one   of   the   most  important   principles   of   unit   testing.   However,   the  CUT  usually  depends  on  other  classes...  

4

Test boundary

Page 5: Automock: Interaction-Based Mock Code Generation

¡  For example, we have a test that access data from a database

5

testMethodA(){ ... insert(“223300”, 20); ... }

Table  Account  [223300,  20]  (1)

(2)

Solution: Initiate the database with necessary data for the test

It can be very slow!!!

Timeout  

Page 6: Automock: Interaction-Based Mock Code Generation

6

Test boundary

Page 7: Automock: Interaction-Based Mock Code Generation

¡  For example, we have tests that access data from a database

7

testMethodA(){ ... insert(“223300”, 20); ... }

Table  Account  [223300,  20]  (1)

(2)

Solution: Initiate the database with necessary data for the test

It can be very slow!!!

Timeout  

Page 8: Automock: Interaction-Based Mock Code Generation

8

Page 9: Automock: Interaction-Based Mock Code Generation

Efficiency

9

Page 10: Automock: Interaction-Based Mock Code Generation

Efficiency

Effectiveness

10

Page 11: Automock: Interaction-Based Mock Code Generation

Efficiency

Effectiveness

Development

11

Page 12: Automock: Interaction-Based Mock Code Generation

12

Development

Maintenance

Efficiency

Effectiveness

Page 13: Automock: Interaction-Based Mock Code Generation

¡  Mocks  can  make  tests  more  effective  and  efficient  

§  Mocks  make   testing  more   effective   because  mock   based  tests  provide  more  precise   information  about  failures  and  defects  

§  Mock   based   tests   use   simplified   simulations   of   the   real  collaborators  

§  They   tend   to   run   faster,   especially   when   collaborators  access   databases,   internet   connections   or   other   external  systems  

13

Page 14: Automock: Interaction-Based Mock Code Generation

¡  Writing   tests   that   use   mocks,   however,   can   be   a  tedious  and  costly  task  

§  A  mock   based   unit   test   consists   in   writing   an   expectation  script  

§  To   be   able   to   do   so,   testers   must   have   a   precise  understanding   of   the   interactions   that   occur   among   all  objects  in  the  chosen  scenario  

§  This   includes   not   only   the   sequence   of   calls,   but   also   the  precise  data  that  is  sent  and  received  back  in  each  call  

§  Mock-­‐based  tests  are  short-­‐lived,  they  must  be  reconfigured  for  every  minor  design  change  of  the  objects  in  the  scenario  

14

Page 15: Automock: Interaction-Based Mock Code Generation

¡  All  these  drawbacks  make  using  mocks  costly  

¡  As  a  consequence,   in  practice,  testers  must  do  cost-­‐benefit  analysis,  and  mock  only  part  of  the  unit  tests,  when  they  don´t  give  up!  

15

Page 16: Automock: Interaction-Based Mock Code Generation

¡  In  a  test  package,  the  percentage  of  unit  tests  with  mock  objects  can  be  large  ¡  For   instance,   in   OurBackup   Home   Project,   53.4%   of   all  

automatic  unit  tests  use  mock  objects!  

¡  Thus,  how  can  we   take  advantage  of  mock  objects  without  the  burden  caused  by  the  development  and  maintenance?  

16

Page 17: Automock: Interaction-Based Mock Code Generation

Development

Maintenance

Efficiency

Effectiveness

17

Page 18: Automock: Interaction-Based Mock Code Generation

Development Efficiency

Effectiveness

18

Page 19: Automock: Interaction-Based Mock Code Generation

Efficiency

Effectiveness

19

Page 20: Automock: Interaction-Based Mock Code Generation

Efficiency

Effectiveness

20

Automock  Automates   the   writing   of  mock  objects,  by  generating  them   automatically   for   a  given  test  class!  

Page 21: Automock: Interaction-Based Mock Code Generation

21

Automock

Test Collaborators

C1  

C2  

Sta0c  Analysis  

Dynamic  Analysis  

Mock  Code  Genera0on  

Interac0ons Mocked    Unit  Test

Page 22: Automock: Interaction-Based Mock Code Generation

22

Automock

Test Mocked    Unit  Test

Collaborators

C1  

C2  

Sta0c  Analysis  

Dynamic  Analysis  

Mock  Code  Genera0on  

Interac0ons

Page 23: Automock: Interaction-Based Mock Code Generation

¡  Questions:  

§  What   is   the   reduction   of   tester’s   effort,   by   using  Automock?  

§  What  is  the  reduction  of  mock  code  development  time,  by  using  Automock?  

§  Does  the  generated  code  have  the  same  semantic  of  one  developed  manually?  

23

Page 24: Automock: Interaction-Based Mock Code Generation

24

GOAL   To   investigate   the   tester’s   effort   reduction   on   develop  mock   code,   by  comparing    the  number  of  lines  of  test  code  with  and  without  mocks  

QUESTION   What  is  the  reduction  of  tester’s  effort,  by  using  Automock?  

METRIC   Effort’s  reduction  =  (LOTCM  -­‐  LOTC)  /  LOTCM    

Test  Class   LOTC   LOTCM  

Test1 243 600

Test2 905 2300 LOTC: lines of test code without mock objects LOTCM: lines of test code with mock objects

It  means  that  the  tester/programmer  will  only  have  to  produce  the  test  code  normally,  and  then  

generate  mock  code  using  Automock  

Page 25: Automock: Interaction-Based Mock Code Generation

25

GOAL   To   examine   the   mock   code   development   time   through   the   comparison  between  develop  mocks  manually  and  automatically,  by  using  Automock  

QUESTION   What   is   the   reduction   of   mock   code   development   time,   by   using  Automock?  

METRIC   Time  reduction  =  (TMM  -­‐  TMA)  /  TMM  

Test  Class   TMM   TMA  

Test1 30.33 min 15 min

Test2 6 hours 20 min TMM: time to develop mock code manually TMA: time to develop mock code automatically

It  means  that  the  tester/programmer  will  only  take  4%  of  the  time  to  produce  mock  code,  by  

using  Automock  

Page 26: Automock: Interaction-Based Mock Code Generation

¡  Does   the   generated   code   have   the   same   semantic   of   one  developed  manually?  

§  To  apply  mutation  tests  in  a  manual  mock  code  and  in  the  one  automatically  generated  

§  To   do   a   qualitative   analysis,   by   comparing   manually   the  generated  code  and  the  one  developed  in  a  manual  way  

§  To  execute  the  generated  code  and  the  one  developed  in  a  manual  way  with  the  purpose  of  obtain  the  same  result  

26

Page 27: Automock: Interaction-Based Mock Code Generation

¡  We   presented   a   technique   that   automatically  generates  mock  code  for  tests  

¡  In  order   to  support  and  evaluate  the  technique,  we  developed  a  prototype-­‐tool   and  applied   it   in   a   test  development  environment  

¡  Although   the  evaluation   can  only  be   considered  as  preliminary,  the  results  are  promising  

¡  Furthermore,   testers   were   convinced   that   the  technique   can   be   very   helpful   during   test  development  

27

Page 28: Automock: Interaction-Based Mock Code Generation

¡  Future  work  will  be  focused  in  two  directions:  

§  First,  we  will  further  evaluate  the  gains  that  can  be  derived  by   applying   the   technique   by   means   of   more   rigorous  experiments  

§  Second,  we  plan  on  evolving  the  tool  to  make  it  both  more  efficient   and   easier   to   use,   possibly   by   developing   an  Automock  plug-­‐in  to  the  Eclipse  IDE  

28

Page 29: Automock: Interaction-Based Mock Code Generation

Questions?  Suggestions?!  

29

Sabrina  de  F.  Souto,  UFCG,  Brazil  

Student  Forum