Top Banner
Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer
27

Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

May 28, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

Mutation Testing as a Safety Net for

Test Code Refactoring

Ali ParsaiAlessandro MurgiaQuinten D. SoetensSerge Demeyer

Page 2: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

2

“Refactoring is changing a system to improve its internal structure without altering its external behavior.” – Fowler et al.

Page 3: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

3

Does refactoring preserve the behavior?

Perform refactoring

Run the test suite

Do all tests pass?

Production Code

Page 4: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

4

Does refactoring preserve the behavior?

Perform refactoring

Test Code

Page 5: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

5

SolutionA metric to verify the preservation of the test

suite behavior pre- and post- refactoring

Page 6: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

6

Mutation Coverage

Branch Coverage

Options

Page 7: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

7

Branch Coverage

33%

Page 8: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

8

Mutation CoverageProcedure

Example

75%

Page 9: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

9

Branch Coverage+ Can be calculated fast+ Very good tool support – Easily integrable – Not accurate for defining behavior

Page 10: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

10

Mutation Coverage+ More accurate + Adaptable by customizing the fault model – Computationally intensive – Poor tool support, hard to integrate

Page 11: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

Works on Java source code

Works regardless of build system and testing framework

11

Page 12: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

12

Main

JavaRead JavaParse JavaMutate ReportGenerator

Antlr-generatedJava Parser

CustomizedAntlr4 Runtime

Page 13: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

13

LittleDarwin Project Report

Page 14: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

14

LittleDarwin File Report

Page 15: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

15

Mutation testing tool → LittleDarwinBranch coverage tool → JaCoCoToy example → SalaryReal project → AddThis Codec

Experiments

Page 16: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

16

Salary class diagram

Toy Example

Page 17: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

17

Toy Example

Page 18: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

18

Toy Example

Pre- Refactoring

Page 19: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

19

Toy Example

Post- Refactoring

Page 20: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

20

Branch coverage and percentage of passed tests remain the sameMutation coverage changes

Toy Example

Page 21: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

21

The mutant that makes a difference can be traced back to the problematic test

Toy Example

Page 22: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

Pre- Version

22

Real Project

Found a version with refactorings in the test code

Identified the refactorings using RefFinder

Backported the changes in the test code to the previous version

ProductionCode

TestCode

Post- Version

ProductionCode

BackportedTest Code

Page 23: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

23

Real Project

Page 24: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

24

Mutation Coverage remains the same for all classes

Real Project

Page 25: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

Limitations

25

One can catch smaller fishes with a tighter net

Page 26: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

ConclusionMutation testing can act as a safety net for test suite refactoring by detecting behavior changes

26

Mutation testing cannot guaranty the preservation of the test suite behavior in every situationThis method can be used to verify automated refactoring tools

Page 27: Mutation Testing as a Safety Net for Test Code Refactoring · Mutation Testing as a Safety Net for Test Code Refactoring Ali Parsai Alessandro Murgia Quinten D. Soetens Serge Demeyer.

27

Does refactoring preserve the behavior?

Perform refactoring

Test Code

pre- and post- refactoring

Does the mutation coverage stay the

same?