`
cywhoyi
  • 浏览: 421227 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Mutation analysis of Java programs with PIT

 
阅读更多

Software testing aims at checking the correctness of a program. But how can you check the correctness of your tests? Quis custodiet ipsos custodes? Mutation analysis can help you evaluate the quality of a test suite.

The basic principle of mutation analysis is to insert faults into a program, then run the test suite to check if the faults are detected. First, mutation operators create different versions of the program (called mutants), where specific kind of faults have been inserted. These faults usually mimics faults often made by programmers. Then the test suite is run on each mutant. If the test suite can detect the inserted fault the mutant is considered killed. The result of the analysis is the mutation score, which is the percentage of mutants that have been killed.

Why not just cover all the branches?

Mutation analysis could be considered as a test criterion (“test until all mutants are killed”). Then how does it compare to other test criteria, such as branch coverage? This is a tricky question because the mutation operators that are used have a huge impact, but to kill a mutant you not only need to execute the instruction where the fault is inserted, you also need an oracle able to detect this fault.

You should do both. Structural coverage criteria are fast to evaluate, allowing you to have a fast feedback, and force you to cover instructions where no faults have been inserted by the mutation operators. Mutation analysis is dynamic and takes longer to evaluate (in the worst case scenario, you need to execute all the test cases on all the mutants), but your test suite will be able to detect specific kind of faults.

PIT – bytecode-based mutation analysis

PIT is a tool for the mutation analysis of Java programs. It works on bytecode and in memory, which means that it is rather fast (all things considered) and you do not have to manage extra versions of your source code. PIT requires Java 5 or above, and works with JUnit 4 or TestNG 6. Note that as JUnit 4 is able to run JUnit 3 test cases, you can still use PIT with legacy JUnit 3 tests. Another interesting feature of PIT is that it first measure the coverage of your test cases so it will only run the test cases that cover the mutated instruction. This means faster execution time, especially if your tests have a low coverage or your code.

PIT can be executed from command line, with ant, or maven. There are several options available, which lets you specify the classes to mutate, the operators to use, the tests to run, the output format (html, csv, or xml – default is html), etc. It is possible to exclude some methods, or even some method calls (for instance if you do not want to test non-functional calls). There is also a Gradle plugin, an Eclipse plugin, and a Sonar plugin, all developed by third parties.

Examples in this section are from “Game of Life“, an open-source demonstration project for the Jenkins: The Definitive Guide book.

Initialization with maven

First, you need to add the PIT maven plugin in you pom.xml configuration. (Make sure you are using the latest version!)

The <configuration> tag can contain all the options you need (see the documentation for more information).

Running PIT

To run PIT you just need to execute the “org.pitest:pitest-maven:mutationCoverage” goal. The console will show the results, but a detailed report in the specified format can be found in “target/pit-reports/YYYYMMDDHHmm” (as long as you don’t clean target, you can keep several reports).

HTML Report

The index of the report shows the mutation score (the percentage of killed mutants, called mutation coverage here) as well as the line coverage. There is also a summary for each package.

You can also view the result for each package and for each class. Here all the mutants have been killed, except one in GridWriter.

To have more information on the surviving mutant, we need to go to the class view, which gives line by line information on the mutants and the coverage of the tests.

A note on the left of line indicates how many mutants have been inserted, with a more detailed report at the end of the page. Here we can see that two mutants were introduced at line 14 of “GridWriter.java”, one is killed but the other survived. The surviving mutant has been created by the “conditionals boundary mutator”, which means that this code:

has been replaced by this code:

Here the mutant survived, which means that no test case run on this code has failed. It could mean that there are no test cases where row.length is zero (test data problem), or it could mean that there is such a test case, but that its assertions are not able to detect that the instruction in the block has been executed (oracle problem).

Equivalent mutants

Equivalent mutants is one of the most difficult problem when dealing with mutation analysis, as it is undecidable in the general case. An equivalent mutant is a mutant that cannot be distinguished from the original program. For instance these two snippets are equivalent:

 

One particular case where an equivalent mutants could appear is with non-functional code such as the use of a logging framework. It is possible to filter out calls to some method, and PIT already excludes calls to major logging frameworks.

What to do next?

I encourage you to experiment with mutation testing. It will give you a new point of view on your tests, and will allow you to improve the test data as well as the oracles. Take a look at the list of mutation operatorsimplemented by PIT, to have an idea of the kind of faults it will force you to detect. Also, PIT is not the only framework for mutation testing of Java program, nor the first. If you are interested there is a detailed comparison on the PIT website.

分享到:
评论

相关推荐

    PIT-mutation-testing:https

    总的来说,PIT-mutation-testing是一个针对Java项目进行变异测试的实践,通过Gradle集成PIT插件,可以有效地提升测试的质量,确保代码的健壮性,并有助于减少bug的遗留。对于大型项目或团队来说,这是一种非常有价值...

    pit-idea-plugin:IntelliJ Idea插件,用于PIT突变测试

    "pitest"和"pitest-plugin"是与PIT测试框架相关的标签,PIT(Pitest或Premier Inn Test)是Java生态中的一个流行突变测试工具,它能够快速有效地生成代码突变并评估测试的质量。“Java”标签表明这个插件和PIT工具...

    Java+9+Programming+By+Example-Packt+Publishing(2017).pdf

    Java drastically changed with the introduction of Java 8, and this change has been elevated to a whole new level with the new version, Java 9. Java has a well-established past, being more than 20 ...

    dotnext2019-mutation-analysis:突变分析示例应用

    dotnext 2019 MSK突变分析 文学 推介会 运行示例 ./scripts/test.coverage.sh cd test/ dotnet tool install -g dotnet-stryker dotnet stryker -cp stryker-config.json 变异分析 让我们看一个最简单的例子 ...

    sanger测序突变检测Mutationsurveyor

    sanger测序突变检测Mutationsurveyor

    Vue父组件向子组件传值以及data和props的区别详解

    1.在父组件中定义 msg 属性 data:{ msg:'123 -我是父组件中的数据' }, 2.引用子组件 父组件可以在引用子组件的时候,通过属性绑定的形式,把需要传递给子组件的数据,以属性绑定的形式,传递到子组件内部,供子...

    PSO with adaptive mutation and inertia weight and its applica源代码

    A., PSO with adaptive mutation and inertia weight and its application in parameter estimation of dynamic systems, Acta Automatica, vol. 37, no. 5, pp. 541-549, 2011." matalb 2014 "main.m" is the ...

    A novel mutation of the POFUT1 gene in a Chinese family with Dowling-Degos Disease

    1例中国Dowling-Degos家系POFUT1基因新突变,张国龙,杜旭峰,目的:探索1例中国Dowling-Degos家系的发病原因;方法:收集该家系患者及正常个体的静脉外周血2ml,利用聚合酶链反应和Sanger测序技术对K

    Comparative Analysis of Genetic Algorithm Implementations

    遗传算法通过基本的遗传算子,包括交叉(crossover)和变异(mutation)来进化解决方案,这个进化过程从一组潜在解组成的种群开始。 当实现遗传算法时,需要为特定的问题领域定制参数和数据结构。这个实现通常涉及...

    NSGAII MOEA java源代码

    在`moea`这个压缩包中,可能包含了实现上述功能的Java源文件,例如`.java`文件,可能有`Population.java`, `Individual.java`, `Fitness.java`, `Crossover.java`, `Mutation.java`, `Selection.java`以及`NSGAII....

    chapter5_matlab;遗传算法_搜索算法_mutation_

    在MATLAB中,变异概率(Mutation Rate)和变异强度(Mutation Scale Factor)是两个重要参数,它们影响着变异的效果。 4. **适应度函数**(Fitness Function):适应度函数用于评估每个个体的优劣,通常与目标函数...

    Differential-Mutation-Analysis:差异变异分析

    差异变异分析差异突变分析是一个框架,可通过比较癌症基因组中基因的突变图谱与健康个体中其自然种系变异图谱来发现癌症基因。 如果您想尝试一下,请访问 。 如果您使用我们的方法,请引用Pawel Przytycki和Mona ...

    Mutation_遗传算法变异_

    Mutation_遗传算法变异_是一种在优化问题中广泛应用的计算方法,源自生物进化理论中的自然选择和遗传机制。在遗传算法中,"变异"是关键步骤之一,它有助于保持种群的多样性,避免早熟和陷入局部最优解。下面将详细...

    graphql-java是GraphQL的一个Java实现

    GraphQL的Java实现——graphql-java,使得Java开发者能够轻松地在后端集成GraphQL服务。 ### GraphQL的基本概念 1. **Schema**: GraphQL服务的核心是Schema,它定义了客户端可以查询和修改的数据类型、字段以及...

    EWA.zip_The One And Only_cauchy mutation_earthworm_metaheuristic

    Earthworms can aerate the soil with their burrowing action and enrich the soil with their waste nutrients. Inspired by the earthworm contribution in nature, a new kind of bio-inspired metaheuristic ...

    matlab代码注释标准-Mutation-Analysis:突变分析

    Mutation-Analysis 说明 目前包括的模块 基础模块:样本路径查找、QC Calling:Mutect2、cnvkit 分析模块:Mutect2结果过滤、oncotator、MutSigCV、GISTIC、HotNet2 需要提供的列表在Summary文件夹中均有示例 可以在...

    Java MUtation Testing for Eclipse-开源

    Eclipse的Java突变测试插件,JMuTE(Java Mutation Testing for Eclipse),就是这样一个工具,它为开发者提供了一种方便的方式来执行突变测试,以确保他们的JUnit测试用例足够强大。 首先,让我们深入了解突变测试...

    Effects of truncated mutation of APC on cell-matrix and cell-cell adhesion in kidney epithelial cell lines

    APC截短突变对MDCK细胞-基质、细胞-细胞粘附的影响作用,李文玲,祝文思,目的 探讨结肠腺瘤息肉蛋白APC截短突变对细胞-细胞、细胞-基质之间粘附的影响作用及具体的机制。方法 应用细胞粘附测定实验检验MDCK

    EGFR mutation lung cancer

    ### EGFR突变型非小细胞肺癌:吉非替尼与顺铂加多西他赛对比研究 #### 背景介绍 表皮生长因子受体(EGFR)是一种跨膜蛋白,在多种类型的癌症中发挥关键作用,尤其是非小细胞肺癌(NSCLC)。EGFR的基因突变在非小...

Global site tag (gtag.js) - Google Analytics