Improve your tests with Mutation Testing. Nicolas Fränkel

Similar documents
Content. Development Tools 2(63)

Improving Software Quality with the Continuous Integration Server Hudson. Dr. Ullrich Hafner Avaloq Evolution AG 8911

TeamCity A Professional Solution for Delivering Quality Software, on Time

Jenkins User Conference Herzelia, July #jenkinsconf. Testing a Large Support Matrix Using Jenkins. Amir Kibbar HP

Unit Testing. and. JUnit

Table of Contents. LESSON: The JUnit Test Tool...1. Subjects...2. Testing What JUnit Provides...4. JUnit Concepts...5

CI/CD Cheatsheet. Lars Fabian Tuchel Date: 18.March 2014 DOC:

Build management & Continuous integration. with Maven & Hudson

Software Quality Exercise 2

Software infrastructure for Java development projects

Comparing the effectiveness of automated test generation tools EVOSUITE and Tpalus

Builder User Guide. Version Visual Rules Suite - Builder. Bosch Software Innovations

Upping the game. Improving your software development process

Sonatype CLM for Maven. Sonatype CLM for Maven

Java Forum Nord Dirk Mahler

CS 451 Software Engineering Winter 2009

Software project management. and. Maven

Development Environment and Tools for Java. Brian Hughes IBM

Continuous Integration Multi-Stage Builds for Quality Assurance

Java Software Quality Tools and techniques

Builder User Guide. Version 5.4. Visual Rules Suite - Builder. Bosch Software Innovations

Maven2. Configuration and Build Management. Robert Reiz

HP Agile Manager What we do

Sonatype CLM Enforcement Points - Continuous Integration (CI) Sonatype CLM Enforcement Points - Continuous Integration (CI)

Server-Side Web Development JSP. Today. Web Servers. Static HTML Directives. Actions Comments Tag Libraries Implicit Objects. Apache.

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

The Benefits of Modular Programming

Automated performance testing using Maven & JMeter. George Barnett, Atlassian Software

CSE 308. Coding Conventions. Reference

Mastering Continuous Integration with Jenkins

Model-View-Controller. and. Struts 2

Paul Barham Program Manager - Java. David Staheli (dastahel@microsoft.com) Software Development Manager - Java

Web Frameworks and WebWork

CI:IRL. By Beth Tucker Long

Domain Specific Languages for Selenium tests

Unit Testing JUnit and Clover

<Insert Picture Here> What's New in NetBeans IDE 7.2

November 12 th 13 th London: Mastering Continuous Integration with Jenkins

Software Construction

TESTING WITH JUNIT. Lab 3 : Testing

Build Management. Context. Learning Objectives

Hands on exercise for

Evaluation of AgitarOne

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Developing Eclipse Plug-ins* Learning Objectives. Any Eclipse product is composed of plug-ins

Why Agile Works: Economics, Psychology, and #PrDC16

CONTINUOUS INTEGRATION

Project 2 Database Design and ETL

Onset Computer Corporation

Maven2 Reference. Invoking Maven General Syntax: Prints help debugging output, very useful to diagnose. Creating a new Project (jar) Example:

WIRIS quizzes web services Getting started with PHP and Java

Software project management. and. Maven

GlassFish v3. Building an ex tensible modular Java EE application server. Jerome Dochez and Ludovic Champenois Sun Microsystems, Inc.

Introduction to Selenium Using Java Language

from Microsoft Office

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Glassbox: Open Source and Automated Application Troubleshooting. Ron Bodkin Glassbox Project Leader

Quality Cruising. Making Java Work for Erlang. Erik (Happi) Stenman

Continuous integration in OSGi projects using Maven (v:0.1) Sergio Blanco Diez

Unit Testing & JUnit

Service Integration course. Cassandra

Easing embedded Linux software development for SBCs

Vendor: Brio Software Product: Brio Performance Suite

Continuous Integration For Fusion Middleware

Software Engineering I (02161)

Consuming and Producing Web Services with WST and JST. Christopher M. Judd. President/Consultant Judd Solutions, LLC

bbc Developing Service Providers Adobe Flash Media Rights Management Server November 2008 Version 1.5

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Announcement. SOFT1902 Software Development Tools. Today s Lecture. Version Control. Multiple iterations. What is Version Control

Spring Security SAML module

Oracle Endeca Information Discovery Integrator

An evaluation of JavaFX as 2D game creation tool

Agile Development with Jazz and Rational Team Concert

Testing Tools Content (Manual with Selenium) Levels of Testing

J a v a Quiz (Unit 3, Test 0 Practice)

Continuous Delivery. Alejandro Ruiz

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Kohsuke Kawaguchi Sun Microsystems, Inc. hk2.dev.java.net, glassfish.dev.java.net. Session ID

CSE 70: Software Development Pipeline Build Process, XML, Repositories

Applets, RMI, JDBC Exam Review

Maven or how to automate java builds, tests and version management with open source tools

Seminar Datenbanksysteme

Effective feedback from quality tools during development

Transcription:

Improve your tests with Mutation Testing Nicolas Fränkel

Me, Myself and I Developer & Architect As Consultant Teacher/trainer Blogger Speaker Book Author @nicolas_frankel 2

Shameless self-promotion @nicolas_frankel 3

My job @nicolas_frankel 4

Many kinds of testing Unit Testing Integration Testing End-to-end Testing Performance Testing Penetration Testing Exploratory Testing etc. @nicolas_frankel 5

Their only single goal Ensure the Quality of the production code @nicolas_frankel 6

The problem How to check the Quality of the testing code? @nicolas_frankel 7

Code coverage Code coverage is a measure used to describe the degree to which the source code of a program is tested --Wikipedia http://en.wikipedia.org/wiki/code_covera ge @nicolas_frankel 8

Measuring Code Coverage Check whether a source code line is executed during a test Or Branch Coverage @nicolas_frankel 9

Computing Code Coverage CC = L executed L total *100 CC: Code Coverage (in percent) L executed : Number of executed lines of code L total : Number of total lines of code @nicolas_frankel 10

Java Tools for Code Coverage JaCoCo Clover Cobertura etc. @nicolas_frankel 11

100% Code Coverage? Is 100% code coverage realistic? Of course it is. If you can write a line of code, you can write another that tests it. Robert Martin (Uncle Bob) https://twitter.com/unclebobmartin/status/ 55966620509667328 @nicolas_frankel 12

Assert-less testing @Test public void add_should_add() { new Math().add(1, 1); } But, where is the assert? As long as the Code Coverage is OK @nicolas_frankel 13

Code coverage as a measure of test quality Any metric can be gamed! Code coverage is a metric Code coverage can be gamed On purpose Or by accident @nicolas_frankel 14

Code coverage as a measure of test quality Code Coverage lulls you into a false sense of security @nicolas_frankel 15

The problem still stands Code coverage cannot ensure test quality Is there another way? Mutation Testing to the rescue! @nicolas_frankel 16

The Cast William Stryker Original Source Code Jason Stryker Modified Source Code a.k.a The Mutant @nicolas_frankel 17

public class Math { public int add(int i1, int i2) { return i1 + i2; } } public class Math { public int add(int i1, int i2) { return i1 - i2; } } @nicolas_frankel 18

Standard testing Execute Test @nicolas_frankel 19

Mutation testing MUTATION Execute SAME Test? @nicolas_frankel 20

Mutation testing Execute SAME Test Mutant Killed Execute SAME Test Mutant Survived @nicolas_frankel 21

Killed or Surviving? Surviving means changing the source code did not change the test result It s bad! Killed means changing the source code changed the test result It s good @nicolas_frankel 22

Test the code public class Math { public int add(int i1, int i2) { return i1 + i2; } } Execute Test @Test public void add_should_add() { new Math().add(1, 1); } @nicolas_frankel 23

Surviving mutant public class Math { public int add(int i1, int i2) { return i1 - i2; } } Execute SAME Test @Test public void add_should_add() { new Math().add(1, 1); } @nicolas_frankel 24

Test the code public class Math { public int add(int i1, int i2) { return i1 + i2; } } Execute Test @Test public void add_should_add() { int sum = new Math().add(1, 1); Assert.assertEquals(sum, 2); } @nicolas_frankel 25

Killed mutant public class Math { public int add(int i1, int i2) { return i1 - i2; } } Execute SAME Test @Test public void add_should_add() { int sum = new Math().add(1, 1); Assert.assertEquals(sum, 2); } @nicolas_frankel 26

Mutation Testing in Java PIT is a tool for Mutation testing Available as Command-line tool Ant target Maven plugin @nicolas_frankel 27

Mutators Mutators are patterns applied to source code to produce mutations @nicolas_frankel 28

PIT mutators sample Name Example source Result Conditionals Boundary > >= Negate Conditionals ==!= Remove Conditionals foo == bar true Math + - Increments foo++ foo-- Invert Negatives -foo foo Inline Constant static final FOO= 42 static final FOO = 43 Return Values return true return false Void Method Call System.out.println("foo") Non Void Method Call long t = System.currentTimeMillis() long t = 0 Constructor Call Date d @nicolas_frankel = new Date() Date d = null; 29

Important mutators Conditionals Boundary Probably a potential serious bug smell if (foo > bar) @nicolas_frankel 30

Important mutators Void Method Call Assert.checkNotNull() connection.close() @nicolas_frankel 31

Remember It s not because the IDE generates code safely that it will never change equals() hashcode() @nicolas_frankel 32

False positives Mutation Testing is not 100% bulletproof Might return false positives Be cautious! @nicolas_frankel 33

Enough talk @nicolas_frankel 34

Simple PIT plugin call mvn \ org.pitest:pitest-maven:mutationcoverage \ -DtimestampedReports=false @nicolas_frankel 35

Excluding some classes mvn \ org.pitest:pitest-maven:mutationcoverage \ -DexcludedClasses=\ org.joda.money.testbigmoney,\ org.joda.money.testcurrencyunit,\ org.joda.money.testmoney \ -DtimestampedReports=false @nicolas_frankel 36

Reports for Sonar mvn \ org.pitest:pitest-maven:mutationcoverage \ -DexcludedClasses=\ org.joda.money.testbigmoney,\ org.joda.money.testcurrencyunit,\ org.joda.money.testmoney \ -DoutputFormats=XML \ -DtimestampedReports=false @nicolas_frankel 37

Sending data to Sonar mvn sonar:sonar \ -Dsonar.pitest.mode=reuseReport @nicolas_frankel 38

Drawbacks Slow Sluggish Crawling Sulky Lethargic etc. @nicolas_frankel 39

Metrics (kind of) On joda-money mvn clean test-compile mvn surefire:test Total time: 2.181 s mvn pit-test... Total time: 48.634 s @nicolas_frankel 40

Why so slow? Analyze test code For each class under test For each mutator Create mutation For each mutation Run test Analyze result Aggregate results @nicolas_frankel 41

Workarounds This is not acceptable in a normal test run But there are workarounds @nicolas_frankel 42

Set mutators <configuration> <mutators> <mutator> CONSTRUCTOR_CALLS </mutator> <mutator> NON_VOID_METHOD_CALLS </mutator> </mutators> </configuration> @nicolas_frankel 43

Set target classes <configuration> <targetclasses> <param>ch.frankel.pit*</param> </targetclasses> </configuration> @nicolas_frankel 44

Set target tests <configuration> <targettests> <param>ch.frankel.pit*</param> </targettests> </configuration> @nicolas_frankel 45

Dependency distance 1 2 @nicolas_frankel 46

Limit dependency distance <configuration> <maxdependencydistance> 4 </maxdependencydistance> </configuration> @nicolas_frankel 47

Limit number of mutations <configuration> <maxmutationsperclass> 10 </maxmutationsperclass> </configuration> @nicolas_frankel 48

Use MutationFilter @nicolas_frankel 49

PIT extension points Must be packaged in JAR Have Implementation-Vendor and Implementation-Title in MANIFEST.MF that match PIT s Set on the classpath Use Java s Service Provider feature @nicolas_frankel 50

Service Provider Inversion of control Since Java 1.3! Text file located in META- INF/services Interface Name of the file Implementation class Content of the file @nicolas_frankel 51

Sample structure JAR ch.frankel.lts.sample @nicolas_frankel 52

Service Provider API @nicolas_frankel 53

Service Provider sample ServiceLoader<ISample> loaders = ServiceLoader.load(ISample.class); for (ISample sample: loaders) { } // Use the sample @nicolas_frankel 54

Output formats Out-of-the-box HTML XML CSV @nicolas_frankel 55

Output formats <configuration> <outputformats> <outputformat>xml</outputformat> <outputformat>html</outputformat> </outputformats> </configuration> @nicolas_frankel 56

@nicolas_frankel 57

Mutation Filter Remove mutations from the list of available mutations for a class @nicolas_frankel 58

@nicolas_frankel 59

@nicolas_frankel 60

Don t bind to test phase! <plugin> <groupid>org.pitest</groupid> <artifactid>pitest-maven</artifactid> <executions> <execution> <goals> <goal>mutationcoverage</goal> </goals> <phase>test</phase> </execution> </executions> </plugin> @nicolas_frankel 61

Use scmmutationcoverage mvn \ org.pitest:pitest-maven:scmmutationcoverage \ -DtimestampedReports=false @nicolas_frankel 62

Do use on Continuous Integration servers mvn \ org.pitest:pitest-maven:mutationcoverage \ -DtimestampedReports=false @nicolas_frankel 63

Is Mutation Testing the Silver Bullet? Sorry, no! It only Checks the relevance of your unit tests Points out potential bugs @nicolas_frankel 64

What it doesn t do Validate the assembled application Integration Testing Check the performance Performance Testing Look out for display bugs Etc. End-to-end testing @nicolas_frankel 65

Testing is about ROI Don t test to achieve 100% coverage Test because it saves money in the long run Prioritize: Business-critical code Complex code @nicolas_frankel 66

Q&A @nicolas_frankel http://blog.frankel.ch/ https://leanpub.com/integ rationtest/ @nicolas_frankel 67