问:如何检测真实的测试覆盖率?
我注意到代码覆盖率指标和测试质量存在一个问题:100% 的代码覆盖率并不意味着代码真正经过测试。
有时测试会提供 100% 的覆盖率,即使它没有覆盖所有内容。问题在于覆盖定义,我们假设coverage==reachable code。
但这不是真的,代码可能 100% 可达,但不能 100% 被测试覆盖。
看看例子,这个测试给出了 100% 的覆盖率 (EMMA),但实际上它没有覆盖将传递给服务模拟的值。因此,如果更改值,测试将不会失败。
例子:
public class User {
public static final int INT_VALUE = 1;
public static final boolean BOOLEAN_VALUE = false;
public static final String STRING_VALUE = "";
private Service service;
public void setService(Service service) {
this.service = service;
}
public String userMethod() {
return service.doSomething(INT_VALUE, BOOLEAN_VALUE, STRING_VALUE);
}
}
并测试它:
public class UserTest {
private User user;
private Service easyMockNiceMock;
@Before
public void setUp() throws Exception {
user = new User();
easyMockNiceMock = EasyMock.createNiceMock(Service.class);
}
@Test
public void nonCoverage() throws Exception {
// given
user.setService(easyMockNiceMock);
expect(easyMockNiceMock.doSomething(anyInt(), anyBoolean(), (String) anyObject())).andReturn("");
replay(easyMockNiceMock);
// when
user.userMethod();
// then
verify(easyMockNiceMock);
}
}
最佳答案
看看Jester , 它执行 mutation testing .来自网站:
Jester finds code that is not covered by tests. Jester makes some change to your code, runs your tests, and if the tests pass Jester displays a message saying what it changed. Jester includes a script for generating web pages that show the changes made that did not cause the tests to fail.
Jester is different than code coverage tools, because it can find code that is executed by the running of tests but not actually tested. Jester's approach is called mutation testing or automated error seeding. However, Jester is not meant as a replacement for code coverage tools, merely as a complementary approach.
关于java - 覆盖率与可达代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10129864/