2016-03-15 106 views
0

我已經從應用程序的實現階段轉移到測試階段。我正在嘗試編寫一些單元測試,但是正在爲這個概念而努力。我已經能夠在android studio中設置並運行樣本單元測試,該測試通過了。Android Studio編寫測試腳本

這是一個簡單的測試,2 + 2的結果等於4.我明白,問題是如何編寫單元測試來測試我的應用程序的更復雜的方法?

以下面的方法爲例,使用JSON將用戶註冊到數據庫。顯然這種方法不如2 + 2簡單,所以如何爲此編寫一個單元測試?

我也有與檢查電子郵件是否有效或不是一個簡單的測試情況下,我得到了錯誤的問題是cannot resolve method 'is(boolean)'

有效的電子郵件測試

public class UnitTest { 

@Test 
public void validEmailTest() throws Exception { 
    assertThat(RegisterActivity.isValidEmail("[email protected]"), is(true)); 
} 
} 

單元測試類

import com.example.test.Activities.RegisterActivity; 
import org.junit.Test; 

import static org.junit.Assert.*; 

public class UnitTest { 


@Test 
public void addition_isCorrect() throws Exception { 
    assertEquals(4, 2 + 2); 
} 

@Test 
public void validEmailTest() throws Exception { 
    assertThat(RegisterActivity.isValidEmail("[email protected]"), is(true)); 
} 
} 

回答

0

看看Getting Started with Testing

你應該特別注意儀器化檢驗,這是一個Android設備或模擬器上運行,因此他們有權訪問測試中應用程序的內部信息的測試。 Instrumented測試可用於單元,用戶界面(UI)或應用程序組件集成測試。

您可以在https://github.com/googlesamples/android-testing

+0

我看看那裏已經,但仍不能確定如何的例子應用到我的代碼 – JJSmith

+0

感謝您的建議,我有alook找到例子,我就更加清晰了一點什麼需要做的,我更新了一個錯誤,我似乎無法擺脫簡單的測試 – JJSmith

+0

使用'assertTrue()' –