簡體   English   中英

如何使用junit和Eclemma增加測試覆蓋率?

[英]How to increase test coverage using junit and Eclemma?

我有一堂簡單的課:

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類:

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%的代碼覆蓋率。 但這到底是什么意思? 我怎樣才能做到這一點? 我已經經營過Eclemma,但我只有46%。

100%的代碼覆蓋率意味着測試覆蓋每一行代碼。

換句話說,您的測試代碼應調用並遍歷已編寫的所有內容,並確保其按預期工作。

在您的情況下,這意味着必須調用所有方法,並且必須測試每個if-else if情況。


即使100%的代碼覆蓋率非常誘人,但最重要的是測試套件的質量。

如果剩下的15%都是一些吸氣劑/設置器,對無法檢查的外部API的調用,非常難於測試的代碼的膠合,則85%的代碼覆蓋率可能接近完美。 在不知道您的應用程序會留下漏洞(和炸彈?)的情況下,您必須意識到可以和應該測試哪些代碼以及可以保留哪些代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM