简体   繁体   English

为什么在测试用例中测量代码覆盖率?

[英]Why is code coverage measured in test cases?

I have measured code coverage using EclEmma. 我已经使用EclEmma测量了代码覆盖率。 I would have expected that only source packages are under test. 我曾希望只有源程序包正在测试中。 Surprisingly JUnit Test Cases are also measured. 令人惊讶的是,还对JUnit测试用例进行了测量。

public class EmailTest {

public static final String VALID_MAIL = "xyz@qwe.abc";

public static final String INVALID_MAIL_WITHOUT_AT = "xyzqwe.abc";

@Test
public void shouldCreateValidEmail() {
    Email email = null;
    try {
        email = new Email (VALID_MAIL);
    } catch (NotValidEmailException e) {
        Assert.fail();
    }
    Assert.assertEquals("Email: " + VALID_MAIL + " should be correct created", VALID_MAIL, email.getEmail());
}

@Test(expected = NotValidEmailException.class)
public void shouldThrowExceptionWhenMailHasNoAt() throws NotValidEmailException {
    new Email (INVALID_MAIL_WITHOUT_AT);
}

Just for example I have a basic Test Case for Email-Regex Validation. 例如,我有一个用于电子邮件正则表达式验证的基本测试用例。 Test Coverage for this test class "EmailTest" is 73%. 此测试类别“ EmailTest”的测试覆盖率为73%。 The Class under test "Email" is in 100% covered. 被测类“电子邮件”的覆盖率为100%。

Why is code coverage in test cases measured and what is that for? 为什么要测量测试用例中的代码覆盖率?这是做什么用的?

Test packages can be from Code Coverage Measure Analysis Scope excluded. 测试包可以从代码覆盖率度量分析范围中排除。

Coverage As -> Coverage Configurations -> Coverage -> Analysis Scope 覆盖范围->覆盖范围配置->覆盖范围->分析范围

You can exclude test code from coverage, but you shouldn't. 您可以从覆盖范围中排除测试代码,但不应这样做。 Coverage is designed to tell you important things about your code. 覆盖范围旨在告诉您有关代码的重要信息。 Your test code is also important. 您的测试代码也很重要。 For example, you may have code in your test helpers that is never run: you can delete that code. 例如,您的测试助手中可能包含永远不会运行的代码:您可以删除该代码。 Or you may have accidentally written test lines that are never executed for some reason. 否则,您可能会意外地编写了由于某些原因而从未执行过的测试行。

The only reason to exclude test code is if you have set yourself some arbitrary goal like 75% coverage, and don't want to "cheat" by including your tests. 排除测试代码的唯一原因是,如果您为自己设定了诸如75%的覆盖率之类的任意目标,并且不想通过包含测试来“欺骗”。 There is no meaning to those goals, so don't let them limit what you find out from coverage. 这些目标没有意义,所以不要让它们限制您从覆盖范围中发现的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM