Cobertura單元測試覆蓋率實例


Cobertura 是一種開源工具,它通過檢測基本的代碼,並觀察在測試包運行時執行了哪些代碼和沒有執行哪些代碼,來測量測試覆蓋率。除了找出未測試到的代碼並發現 bug 外,Cobertura 還可以通過標記無用的、執行不到的代碼來優化代碼,還可以提供 API 實際操作的內部信息。

Cobertura跟Maven完美結合,只需要在pom文件中增加一個插件即可:

<plugin>  
         <groupId>org.codehaus.mojo</groupId>  
         <artifactId>cobertura-maven-plugin</artifactId>  
         <version>2.7</version>  
</plugin> 

 

項目開發正確過程應該如此:

1.創建一個User對象:

package jmust.demo.Cobertura;

/**
 * Hello world!
 *
 */
public class User 
{
    private String userName;
    private String password;
    private String nickName;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getNickName() {
        return nickName;
    }
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    
    public User(){
        super();
    }
    
    public User(String userName,String password,String nickName){
        super();
        this.userName = userName;
        this.password = password;
        this.nickName = nickName;
    }
}

2.定義功能接口類:

package jmust.demo.Cobertura.Inter;

import jmust.demo.Cobertura.User;

public interface IUserService {

    public boolean add(User user);
    public boolean delete(String userName);
    public User load(String userName);
    public User login(String userName,String password);
}

3.編寫單元測試類:

package jmust.demo.CoberturaTest;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import jmust.demo.Cobertura.User;
import jmust.demo.Cobertura.Exception.UserException;
import jmust.demo.Cobertura.Inter.IUserService;
import jmust.demo.Cobertura.Service.UserService;

/**
 * Unit test for simple User.
 */
public class UserServiceTest {
   private IUserService us;
   private User user;
   
   @Before
   public void setUp(){
       //初始化
       us = new UserService();
       user = new User("admin","123","管理員");
   }
   
   @Test
   public void addTest(){
       User u = user;
       us.add(u);
       User tu = us.load("admin");
       assertNotNull(tu);
   }
   
   @Test(expected=UserException.class)  
   public void AddExistUsername(){  
       us.add(user);  
       User tu = new User("admin", "1234", "alskdf");  
       boolean state = us.add(tu);  
       assertFalse(state);
   }  
     
   @Test  
   public void testDelete(){  
       us.add(user);  
       User tu = us.load(user.getUserName());  
       assertNotNull(tu);  
       us.delete(user.getUserName());  
       tu = us.load(user.getUserName());  
       assertNull(tu);  
   }  
     
   @Test  
   public void testLogin(){  
       us.add(user);  
       String username=user.getUserName();  
       String password=user.getPassword();  
       User tu = us.login(username, password);  
       assertNotNull(tu);
   }  
     
   @Test(expected=UserException.class)  
   public void testNotExistsUserLogin(){  
       us.add(user);  
       String username="admin1";  
       String password="123";  
       User tu = us.login(username, password);  
       assertNotNull(tu);
   }  
     
   @Test(expected=UserException.class)  
   public void testPasswordErrorUserLogin(){  
       us.add(user);  
       String username="admin";  
       String password="1235";  
       User tu = us.login(username, password);  
       assertNotNull(tu);
   }  
}

4.再編寫功能實現類:

package jmust.demo.Cobertura.Service;

import java.util.HashMap;
import java.util.Map;

import jmust.demo.Cobertura.User;
import jmust.demo.Cobertura.Exception.UserException;
import jmust.demo.Cobertura.Inter.IUserService;

public class UserService implements IUserService{
    
    private Map<String,User> us = new HashMap<String,User>();
    /**
     * 添加
     */
    public boolean add(User user) {
        if(load(user.getUserName()) != null){
            throw new UserException("用戶已存在");
        }
        us.put(user.getUserName(), user);
        return true;
    }
    /**
     * 刪除
     */
    public boolean delete(String userName) {
        us.remove(userName);
        
        return true;
    }
    /**
     * 加載
     */
    public User load(String userName) {
        return us.get(userName);
    }
    /**
     * 登錄
     */
    public User login(String userName, String password) {
        User u = load(userName);
        if(u == null) throw new UserException("用戶不存在");
        if(!u.getPassword().equals(password)) throw new UserException("用戶密碼不正確");
        return u;
    }

}

 

 

 

到此,項目開發完成了。然后我們來執行,看看它的覆蓋率如何:

首先,我們先來把項目打包成jar包,通過maven命令執行,  mvn install或者mvn package或者mvn deploy

然后,我們再繼續執行maven命令,mvn cobertura:cobertura ,完成后,會在項目的目錄target下有個site文件夾,我們打開里面的index頁面,可以看到詳細的覆蓋率以及代碼執行次數等。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM