首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用junit和Eclemma来增加测试覆盖率?

如何使用junit和Eclemma来增加测试覆盖率?
EN

Stack Overflow用户
提问于 2014-10-10 13:44:53
回答 1查看 6.6K关注 0票数 0

我有一个简单的课程:

代码语言:javascript
复制
public class NPP {

    // inputs
    public int m__water_pressure = 0;
    public boolean m__blockage_button = false;
    public boolean m__reset_button = false;

    public int old_m__pressure_mode = 0;
    public boolean old_m__reset_button = false;
    public boolean old_m__blockage_button = false;
    public int old_m__water_pressure = 0;

    // outputs
    public int c__pressure_mode = 0;
    public boolean c__the_overriden_mode = false;
    public int c__the_safety_injection_mode = 0;

    public int p__pressure_mode = 0;
    public boolean p__the_overriden_mode = false;
    public int p__the_safety_injection_mode = 0;    

    public void method__c__pressure_mode() {
        if ( m__water_pressure >= 9 && old_m__water_pressure < 9 && c__pressure_mode == 0 ) {
            p__pressure_mode = 1;
        } else if ( m__water_pressure >= 10 && old_m__water_pressure < 10 && c__pressure_mode == 1 ) {
            p__pressure_mode = 2;
        } else if ( m__water_pressure < 9 && old_m__water_pressure >= 9 && c__pressure_mode == 1 ) {
            p__pressure_mode = 0;
        } else if ( m__water_pressure < 10 && old_m__water_pressure >= 10 && c__pressure_mode == 2 ) {
            p__pressure_mode = 1;
        }
    }

    public void method__c__the_overriden_mode() {
        if ( m__blockage_button == true && old_m__blockage_button == false && m__reset_button == false && !(c__pressure_mode==2) ) {
            p__the_overriden_mode = true;
        } else if ( m__reset_button == true && old_m__reset_button == false && !(c__pressure_mode==2) ) {
            p__the_overriden_mode = false;
        } else if ( c__pressure_mode==2 && !(old_m__pressure_mode==2) ) {
            p__the_overriden_mode = false;
        } else if ( !(c__pressure_mode==2) && old_m__pressure_mode==2 ) {
            p__the_overriden_mode = false;
        }
    }

    public void method__c__the_safety_injection_mode() {
        if ( c__pressure_mode == 0 && c__the_overriden_mode == true ) {
            p__the_safety_injection_mode = 0;
        } else if ( c__pressure_mode == 0 && c__the_overriden_mode == false ) {
            p__the_safety_injection_mode = 1;
        } else if ( c__pressure_mode == 1 || c__pressure_mode == 2 ) {
            p__the_safety_injection_mode = 0;
        }
    }

}

我写了这个junit类:

代码语言:javascript
复制
import static org.junit.Assert.*;

import org.junit.Test;


public class NPPTest {

    @Test
    public void testMethod__c__pressure_mode() {
        NPP npp = new NPP();
        npp.m__water_pressure  = 3;
        npp.old_m__water_pressure = 5;
        npp.c__pressure_mode = 2;
        npp.method__c__pressure_mode();
        assertEquals(1, npp.p__pressure_mode);          
    }

    @Test
    public void testMethod__c__the_overriden_mode() {
        NPP npp = new NPP();
        npp.m__blockage_button = false;
        npp.old_m__blockage_button = true;
        npp.m__reset_button = false;
        npp.method__c__the_overriden_mode();
        assertFalse(npp.p__the_overriden_mode);

    }

    @Test
    public void testMethod__c__the_safety_injection_mode() {
        NPP npp = new NPP();
        npp.c__pressure_mode = 2;
        npp.c__the_overriden_mode = false;
        npp.method__c__the_safety_injection_mode();
        assertEquals(1, npp.p__the_safety_injection_mode);

    }

}

我被要求编写一些测试,并覆盖100%的代码覆盖率。但这到底意味着什么呢?我怎样才能做到这一点?我查过伊克勒玛,只有46%。

EN

回答 1

Stack Overflow用户

发布于 2014-10-10 13:50:46

100%的代码覆盖率意味着每一行代码都包含在测试中。

换句话说,您的测试代码应该调用并遍历已经编写的所有内容,并确保它按预期工作。

在您的情况下,这意味着必须调用所有方法,并且必须测试所有if-else (如果是case )。

尽管100%的代码覆盖率非常性感,但最重要的是测试套件的质量。

如果剩下的15%是一些getter/setter,调用外部API(无法检查),粘合代码(非常难以测试),那么85%的代码覆盖率可能接近完美。这取决于你意识到哪些代码可以并且应该被测试,哪些代码可以在不知道你正在留下漏洞(和炸弹)的情况下留下?在你的应用程序里。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26300945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档