2017-04-10 144 views
-1

我在android中製作了一個示例應用程序,並在其中包含了aar文件,並且我已經爲應用程序執行了單元測試,無論是否可以對示例應用程序執行單元測試?如何在android studio中執行單元測試

+0

從這裏開始? https://developer.android.com/training/testing/start/index.html –

回答

0

考慮下面的示例班單元測試

public class SampleUnitTestClass { 

    public int add(int a, int b) { 
     return a + b; 
    } 

    public int subtract(int a, int b) { 
     return a - b; 
    } 
} 

創建類使用快捷鍵,按Ctrl + Shift + T創建對應於您的樣品類的新測試類之後。

  • 點擊創建新測試
  • 選擇在你的單元測試類所需的方法和(如果需要的話,您也可以改變類名,目的地封裝,測試庫)單擊確定
  • 選擇目的地目錄,然後單擊確定
  • 一個單元測試類將被創建

    public class SampleUnitTestClassTest { 
    @Test 
    public void add() throws Exception { 
    
    } 
    
    @Test 
    public void subtract() throws Exception { 
    
    } 
    

    }

這裏寫你的測試邏輯和資產的answer.For如:

public class SampleUnitTestClassTest { 
@Test 
public void add() throws Exception { 
    SampleUnitTestClass testClass = new SampleUnitTestClass(); 
    int answer = testClass.add(2,7); 
    assertEquals("Addition of 2 positive integers",9,answer); 
} 

@Test 
public void subtract() throws Exception { 
    SampleUnitTestClass testClass = new SampleUnitTestClass(); 
    int answer = testClass.subtract(2,7); 
    assertEquals("Subtraction of 2 positive integers",-5,answer); 
} 

}

添加更多的方法,包括負值,空值等,並斷言答案。

0

對於單元測試,你可以使用Mockito,如果你需要一些Android資源,你也可以閱讀Robolectric。

相關問題