内容简介:01Mutation Test(突变测试) : 一种评估测试质量的的新方法自动植入Faults(or mutations),观察测试执行情况,如果测试失败,则mutation被killed,测试成功则mutation lived。
01
Mutation Test(突变测试) : 一种评估测试质量的的新方法
自动植入Faults(or mutations),观察测试执行情况,如果测试失败,则mutation被killed,测试成功则mutation lived。 PIT是一种突变测试系统,为 Java 和jvm提供黄金标准测试覆盖。它快速,可扩展,并与现代测试和构建 工具 集成。
测试的质量可以通过killed mutation的比率来度量。
PIT针对应用程序代码的自动修改版本运行单元测试。当应用程序代码更改时,它应该产生不同的结果并导致单元测试失败。如果在这种情况下单元测试没有失败,则可能表明测试套件存在问题。
02
Why
传统的测试覆盖率(即行,语句,分支等)仅测量那些代码被执行到。它不会检查测试是否确实能够检测执行代码中的错误。因此,它只能识别绝对未经过测试的代码。
然而,单从覆盖率的角度,不全面的测试仍然可以执行其所有分支。
例如
public static String foo(boolean b) {
if ( b ) {
performVitallyImportantBusinessFunction();
return "OK";
}
return "FAIL";
}
@Test
public void shouldFailWhenGivenFalse() {
assertEquals("FAIL", foo(false));
}
@Test
public void shouldBeOkWhenGivenTrue() {
assertEquals("OK", foo(true));
}
从黑盒的角度,分支覆盖100%,但是没有验证 performVitallyImportantBusinessFunction(); 是否被执行。
public static String foo(int i) {
if ( i >= 0 ) {
return "foo";
} else {
return "bar";
}
}
@Test
public void shouldReturnFooWhenGiven1() {
assertEquals("foo", foo(1));
}
@Test
public void shouldReturnBarWhenGivenMinus1() {
assertEquals("bar", foo(-1));
}
缺少边界验证,i==0。
由于它实际上能够检测每个语句是否经过有意义的测试,因此突变测试是衡量所有其他类型的覆盖范围的黄金标准。
03
samples
针对之前的例子
public class DemoTest {
Demo demo = new Demo();
@Test
public void shouldReturnFooWhenGiven1() {
assertEquals("foo", demo.foo(1));
}
@Test
public void shouldReturnBarWhenGivenMinus1() {
assertEquals("bar", demo.foo(-1));
}
}
public class Demo {
public String foo(int i) {
if ( i >= 0 ) {
return "foo";
} else {
return "bar";
}
}
}
运行mutation coverage
查看报告
line覆盖100%但是mutation75%,说明测试有需要完善的地方。
查看细节
红色表示代码没有被mutation test覆盖,查看细节
补充边界值的验证
@Test
public void checkBoundary() {
assertEquals("foo", demo.foo(0));
}
很多程序问题来源于边界,Mutation test从某种角度增加了我们对边界的思考。
参考
https://gitmaruneko.github.io/2018/03/24/MutationTest.html
http://pitest.org/weak_tests/
THE END
- 晚安 -
图片长按2秒,识别图中二维码,关注订阅号
以上所述就是小编给大家介绍的《PIT -- Real world mutation testing》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Building Social Web Applications
Gavin Bell / O'Reilly Media / 2009-10-1 / USD 34.99
Building a social web application that attracts and retains regular visitors, and gets them to interact, isn't easy to do. This book walks you through the tough questions you'll face if you're to crea......一起来看看 《Building Social Web Applications》 这本书的介绍吧!