who watches the watchmen? - mutation testing

72
Who watches the watchmen?

Upload: rafael-vindel-amor

Post on 22-Jan-2018

181 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Who Watches the Watchmen? - Mutation Testing

Who watches the watchmen?

Page 2: Who Watches the Watchmen? - Mutation Testing

Rafael Vindel Amor

Consultor Tecnológico en @autentia

@rafaelvindel

José Luis Rodríguez Villapecellín

Consultor Tecnológico en @autentia

@jlrv

Page 3: Who Watches the Watchmen? - Mutation Testing

Contexto de las Pruebas

Page 4: Who Watches the Watchmen? - Mutation Testing

¿Qué son las Pruebas?

Page 5: Who Watches the Watchmen? - Mutation Testing

Las pruebas son el proceso de encontrar errores en un

determinado programa.

Page 6: Who Watches the Watchmen? - Mutation Testing

¿Qué tipos de pruebas hay?

https://martinfowler.com/articles/microservice-testing/#conclusion-summary

@jlrv | @rafaelvindel | @MalagaJUG

Page 7: Who Watches the Watchmen? - Mutation Testing

¿para qué sirven?

FAVORECEN EL CAMBIO

FACILITAN LA INTEGRACIÓN

SIRVEN COMO DOCUMENTACIÓN

AUMENTAN LA CALIDAD

@jlrv | @rafaelvindel | @MalagaJUG

Page 8: Who Watches the Watchmen? - Mutation Testing

Cobertura, ¿truco o trato?@Testpublic void is_special_should_return_true_given_an_specific_date() { SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); Date specialDate = dateFormat.parse("26/09/1985");

sut.isSpecialDate(specialDate);}

@jlrv | @rafaelvindel | @MalagaJUG

Page 9: Who Watches the Watchmen? - Mutation Testing

¿Es la cobertura una medida real de

calidad?

Page 10: Who Watches the Watchmen? - Mutation Testing

Somos buenos programadores, ¿verdad?

P

R1

R2

...

Rn

P’

@jlrv | @rafaelvindel | @MalagaJUG

Page 11: Who Watches the Watchmen? - Mutation Testing

¿HA sido probado a fondo?

¿Son unas pruebas adecuadas?

¿Son unas pruebas de calidad?

Page 12: Who Watches the Watchmen? - Mutation Testing
Page 13: Who Watches the Watchmen? - Mutation Testing

Mutation testing

Page 14: Who Watches the Watchmen? - Mutation Testing

¿Qué es el Mutation Testing?

Page 15: Who Watches the Watchmen? - Mutation Testing

Técnica que nos ayuda a mejorar nuestras pruebas y medir su calidad frente a la

cobertura tradicional.

Page 16: Who Watches the Watchmen? - Mutation Testing

¿Cómo funciona?Se realizan cambios en el código, mutaciones, y se ejecutan las pruebas:

Si las pruebas pasan, el mutante sobrevive.

Si las pruebas fallan, el mutante muere.

Al final, nuestras pruebas deberían eliminar todas las mutaciones.

@jlrv | @rafaelvindel | @MalagaJUG

Page 17: Who Watches the Watchmen? - Mutation Testing

¿Qué son las mutaciones?× Modificar operadores

× Modificar constantes

× Modificar variables

× Eliminar llamadas a métodos

× ...

@jlrv | @rafaelvindel | @MalagaJUG

Page 18: Who Watches the Watchmen? - Mutation Testing

Por ejemplo...

@jlrv | @rafaelvindel | @MalagaJUG

Page 19: Who Watches the Watchmen? - Mutation Testing
Page 20: Who Watches the Watchmen? - Mutation Testing

Un poquito de historia...

Primera propuesta realizada por Richard Jay Lipton en 1971 cuando era estudiante:

“Fault Diagnosis of Computer Programs”

Page 21: Who Watches the Watchmen? - Mutation Testing

Un poquito de historia...

No fue hasta 1978 cuando DeMillo, Lipton y Sayward publican:

“Hints on Test Data Selection:

Help for the Practicing Programmer”

Page 22: Who Watches the Watchmen? - Mutation Testing

En 1980, Timothy Budd publica en su doctorado el primer sistema de mutación:

“Mutation Analysis”

Un poquito de historia...

Page 23: Who Watches the Watchmen? - Mutation Testing

¿Por qué No tuvo éxito?× Incapacidad para añadir pruebas unitarias en el

desarrollo.

× Dificultades tecnológicas para automatizar las

pruebas.

× Mutaciones muy costosas computacionalmente.

@jlrv | @rafaelvindel | @MalagaJUG

Page 24: Who Watches the Watchmen? - Mutation Testing
Page 25: Who Watches the Watchmen? - Mutation Testing

Implementaciones

Mutant Humbug

Mutator

Stryker

Mutator

Grunt Mutation Testing

Nester

Visual Mutator

Ninja Turtles

MuCheck

@jlrv | @rafaelvindel | @MalagaJUG

Page 26: Who Watches the Watchmen? - Mutation Testing

Implementaciones

PIT

Jester

MuJava

Javalanche

Major

Jumble

Mutator

Judy

@jlrv | @rafaelvindel | @MalagaJUG

Page 27: Who Watches the Watchmen? - Mutation Testing

IntegracionesCommand Line Maven Gradle IDE SonarQube Concurrency

Javalanche

Jester

PIT

MuJava/MuClipse

Jumble

Mutator

Judy

Major

@jlrv | @rafaelvindel | @MalagaJUG

Page 28: Who Watches the Watchmen? - Mutation Testing

PITJava Mutation System

Page 29: Who Watches the Watchmen? - Mutation Testing

PIT

Herramienta que permite realizar pruebas de mutación de forma automática sobre pruebas en Java.

× Rápido× Fácil de usar× En continuo desarrollo

@jlrv | @rafaelvindel | @MalagaJUG

Page 30: Who Watches the Watchmen? - Mutation Testing

¿Cómo funciona?PIT cambia el bytecode para introducir las mutaciones.

× Números de línea× Nombre del fichero

Para cada mutación, PIT genera una clase Java.

@jlrv | @rafaelvindel | @MalagaJUG

Page 31: Who Watches the Watchmen? - Mutation Testing

× Killed× Lived× No Coverage× Non Viable× Timeout× Memory Error× Run Error

resultados

@jlrv | @rafaelvindel | @MalagaJUG

Page 32: Who Watches the Watchmen? - Mutation Testing
Page 33: Who Watches the Watchmen? - Mutation Testing

condicionales× CONDITIONALS_BOUNDARY: cambia los límites de

las condiciones.

× NEGATE_CONDITIONALS: invierte la lógica de todas las condiciones.

@jlrv | @rafaelvindel | @MalagaJUG

Page 34: Who Watches the Watchmen? - Mutation Testing

× REMOVE_CONDITIONALS: elimina todas las condiciones.

× REMOVE_CONDITIONALS_EQ_IF× REMOVE_CONDITIONALS_EQ_ELSE× REMOVE_CONDITIONALS_ORD_IF× REMOVE_CONDITIONALS_ORD_ELSE

condicionales

@jlrv | @rafaelvindel | @MalagaJUG

Page 35: Who Watches the Watchmen? - Mutation Testing

aritméticas× MATH: cambia las operaciones aritméticas por su

inversa.+ -

* /

& |

<< >>

% *

^ *

>>> <<

@jlrv | @rafaelvindel | @MalagaJUG

Page 36: Who Watches the Watchmen? - Mutation Testing

aritméticas× INCREMENTS: intercambia las operaciones de

incremento y decremento.

× INVERT_NEGS: invierte el signo de los números.

@jlrv | @rafaelvindel | @MalagaJUG

Page 37: Who Watches the Watchmen? - Mutation Testing

otras

× INLINE_CONSTS: cambiar el valor de las asociaciones de las variables.

boolean true por false y viceversa

int byte short 1 por 0, -1 por 1, 5 por -1 y el resto x + 1

long 1 por 0 y el resto x + 1

float 1.0 y 2.0 por 0.0 y el resto por 1.0

double 1.0 por 0.0 y el resto por 1.0

@jlrv | @rafaelvindel | @MalagaJUG

Page 38: Who Watches the Watchmen? - Mutation Testing

otras

× RETURN_VALS: cambia el valor devuelto por los métodos.

boolean true por false y viceversa

int byte short 0 por 1 y el resto por 0

long x 1

float double NAN por 0 y el resto - (x + 1.0)

Object not null por null y RuntimeException por null

@jlrv | @rafaelvindel | @MalagaJUG

Page 39: Who Watches the Watchmen? - Mutation Testing

otras

× VOID_METHOD_CALLS: elimina las invocaciones a todos los métodos que no tengan valor de retorno.

@jlrv | @rafaelvindel | @MalagaJUG

Page 40: Who Watches the Watchmen? - Mutation Testing

otras

× NON_VOID_METHOD_CALLS: reemplaza el valor de los métodos por sus valores por defecto.

boolean false

int byte short long 0

float double 0.0

char ‘\u0000’

Object null

@jlrv | @rafaelvindel | @MalagaJUG

Page 41: Who Watches the Watchmen? - Mutation Testing

otras

× CONSTRUCTOR_CALLS: reemplaza el valor devuelto por los constructores por null.

× EXPERIMENTAL_MEMBER_VARIABLE: reemplaza la inicialización de las variables por sus valores por defecto.

@jlrv | @rafaelvindel | @MalagaJUG

Page 42: Who Watches the Watchmen? - Mutation Testing

otras

× EXPERIMENTAL_SWITCH: cambia el valor devuelto por el switch en función de cada uno de los case.

@jlrv | @rafaelvindel | @MalagaJUG

Page 43: Who Watches the Watchmen? - Mutation Testing

Activadas Desactivadas

CONDITIONALS_BOUNDARYNEGATE_CONDITIONALS

MATHINCREMENTS

INVERT_NEGSRETURN VALS

VOID_METHOD_CALLS

REMOVE_CONDITIONALSINLINE_CONSTS

NON_VOID_METHOD_CALLSCONSTRUCTOR_CALLS

EXPERIMENTAL_SWITCH

RESUMEN

@jlrv | @rafaelvindel | @MalagaJUG

Page 44: Who Watches the Watchmen? - Mutation Testing
Page 45: Who Watches the Watchmen? - Mutation Testing

intellij

@jlrv | @rafaelvindel | @MalagaJUG

Page 46: Who Watches the Watchmen? - Mutation Testing

maven<build> <plugins> <plugin> <groupId>org.pitest</groupId> <artifactId>pitest-maven</artifactId> <version>1.2.0</version> </plugin> </plugins></build>

mvn org.pitest:pitest-maven:mutationCoverage

mvn org.pitest:pitest-maven:scmMutationCoverage

@jlrv | @rafaelvindel | @MalagaJUG

Page 47: Who Watches the Watchmen? - Mutation Testing

propiedades× reportsDirectory× outputFormat× targetClasses× targetTests× threads× mutators× mutationThreshold× avoidCallsTo× verbose

@jlrv | @rafaelvindel | @MalagaJUG

Page 48: Who Watches the Watchmen? - Mutation Testing

Sonar

@jlrv | @rafaelvindel | @MalagaJUG

Page 49: Who Watches the Watchmen? - Mutation Testing

Place your screenshot here{Let’s Code!}

Page 50: Who Watches the Watchmen? - Mutation Testing
Page 51: Who Watches the Watchmen? - Mutation Testing

Análisis Incremental× Si se detecta un bucle infinito y no se cambia la

clase, se sigue asumiendo el bucle infinito.

× Si se elimina una mutación y no se cambia la clase ni la prueba, se sigue asumiendo que se elimina.

@jlrv | @rafaelvindel | @MalagaJUG

Page 52: Who Watches the Watchmen? - Mutation Testing

Análisis Incremental× Si una mutación sobrevive y no se cambia la

clase ni su cobertura, se sigue asumiendo que sobrevive.

@jlrv | @rafaelvindel | @MalagaJUG

Page 53: Who Watches the Watchmen? - Mutation Testing

Análisis Incremental× withHistory: para activarlo o desactivarlo.× historyInputFile: fichero de donde leer el análisis

anterior.× historyOutputFile: fichero donde escribir el

resultado del análisis actual (puede ser igual al anterior).

@jlrv | @rafaelvindel | @MalagaJUG

Page 54: Who Watches the Watchmen? - Mutation Testing
Page 55: Who Watches the Watchmen? - Mutation Testing

mutation result listener× Recibe los resultados de las pruebas y se

configura en outputFormats.

× Para añadirlo, implementar:

org.pitest.mutationtest.MutationResultListenerFactory

@jlrv | @rafaelvindel | @MalagaJUG

Page 56: Who Watches the Watchmen? - Mutation Testing

mutation filter× Se pueden eliminar las mutaciones generadas

antes de ser ejecutadas sobre las pruebas.

× Para añadirlo, implementar:

org.pitest.mutationtest.filter.MutationFilterFactory

@jlrv | @rafaelvindel | @MalagaJUG

Page 57: Who Watches the Watchmen? - Mutation Testing

test prioritiser× Se puede personalizar el orden de ejecución de

las pruebas contra las mutaciones.

× Para añadirlo, implementar:

org.pitest.mutationtest.build.TestPrioritiserFactory

@jlrv | @rafaelvindel | @MalagaJUG

Page 58: Who Watches the Watchmen? - Mutation Testing

mutation engine× Se puede proporcionar un motor de mutación

personalizado indicado por mutationEngine.

× Para más información, consultar:

org.pitest.mutationtest.engine.gregor.*

@jlrv | @rafaelvindel | @MalagaJUG

Page 59: Who Watches the Watchmen? - Mutation Testing

Conclusiones

Page 60: Who Watches the Watchmen? - Mutation Testing

Ventajas

× Ayuda a mejorar la calidad de las pruebas.

× Ayuda a detectar errores de forma y contenido en las pruebas.

@jlrv | @rafaelvindel | @MalagaJUG

Page 61: Who Watches the Watchmen? - Mutation Testing

inconvenientes× Elevado tiempo de ejecución:

× Volumen de código a ser analizado.× Conjunto de mutaciones a realizar.× La cantidad de test que haya que ejecutar.

× Los resultados deben ser correctamente interpretados:

× Falsos positivos.

@jlrv | @rafaelvindel | @MalagaJUG

Page 62: Who Watches the Watchmen? - Mutation Testing

Ejemplos de tiempos de ejecución

@jlrv | @rafaelvindel | @MalagaJUG

Page 63: Who Watches the Watchmen? - Mutation Testing

frente a los altos tiempos de Ejecución...

× Acotar el conjunto de clases mutadas.

× Acotar el conjunto de pruebas ejecutados.

× Análisis incrementales.

@jlrv | @rafaelvindel | @MalagaJUG

Page 64: Who Watches the Watchmen? - Mutation Testing

frente a los Falsos positivos...

× Puede desenmascarar pruebas ambiguas por lo

que mejora también la calidad de las pruebas.

@jlrv | @rafaelvindel | @MalagaJUG

Page 65: Who Watches the Watchmen? - Mutation Testing

Resumiendo...× ¿Es la cobertura una medida real de calidad?

× Altamente falsificada, no representa un valor real.

@jlrv | @rafaelvindel | @MalagaJUG

Page 66: Who Watches the Watchmen? - Mutation Testing

Resumiendo...× ¿Ha sido probado a fondo? ¿Son pruebas

adecuadas? ¿Son pruebas de calidad?

× Puede que sí o puede que no, pero ahora lo sabemos.

@jlrv | @rafaelvindel | @MalagaJUG

Page 67: Who Watches the Watchmen? - Mutation Testing

¡gracias!

Page 68: Who Watches the Watchmen? - Mutation Testing

Be One Of Us

[email protected]● Horario flexible● Aprendizaje continuo● Equipo joven y proactivo● Herramientas de última generación● ...

Page 69: Who Watches the Watchmen? - Mutation Testing

Who watches the watchmen?

Page 70: Who Watches the Watchmen? - Mutation Testing

Enlaces de Interés y Referencias Utilizadas (I)× https://disciplinas.stoa.usp.br/pluginfile.php/1943431/mod_resou

rce/content/1/Hints_on_Test_Data_Selection-Demillo.pdf× https://www.computer.org/csdl/mags/co/1978/04/01646911.pdf× http://cpsc.yale.edu/sites/default/files/files/tr155.pdf× https://stryker-mutator.github.io/× http://jester.sourceforge.net/× http://jumble.sourceforge.net/× http://pitest.org/

@jlrv | @rafaelvindel | @MalagaJUG

Page 71: Who Watches the Watchmen? - Mutation Testing

Enlaces de Interés y Referencias Utilizadas (II)× http://javalanche.org/× https://github.com/david-schuler/javalanche× https://cs.gmu.edu/~offutt/mujava/× http://www.mutationtesting.org/× http://mutation-testing.org/× https://github.com/mbj/mutant× https://github.com/padraic/humbug× http://ortask.com/mutator/

@jlrv | @rafaelvindel | @MalagaJUG

Page 72: Who Watches the Watchmen? - Mutation Testing

Enlaces de Interés y Referencias Utilizadas (III)× https://github.com/stryker-mutator/stryker× https://www.npmjs.com/package/grunt-mutation-testing× http://nester.sourceforge.net/× https://visualmutator.github.io/web/× https://ninjaturtles.codeplex.com/× https://hackage.haskell.org/package/MuCheck

@jlrv | @rafaelvindel | @MalagaJUG