Android單元測試
創(chuàng)建android studio工程,在app目錄下的build.gradle中加入JUnit4的依賴氮惯。
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0-beta1'
}
創(chuàng)建Unit Test
創(chuàng)建測試類Tools
public class Tools {
public double sum(double a ,double b){
return 0;
}
public double abs(double value){
return 0;
}
}
在待測試的類(Tools)代碼編輯器內(nèi)八回,右鍵選擇Go to -> Test
或者使用快捷鍵Ctrl + Shift + T更啄,Create New Test 。
Testing library選擇JUnit4
Generate選擇setUp/@Before
Destionation package選擇生成的測試類的包路徑
如下圖:
創(chuàng)建成功后會在app/src/test/java/me/peace/junit下生成測試類(me/peace/junit即Destionation package)
一般與javas相關(guān)的單元測試選擇test/java目錄挽荠,與android相關(guān)的選擇androidTest/java进栽,若缺少這兩個目錄請自行創(chuàng)建
在這個測試類中加入具體的測試內(nèi)容
public class ToolsTest {
private Tools mTools;
@Before
public void setUp() throws Exception {
mTools = new Tools();
System.out.println("Before - setUp");
}
@Test
public void testSum() throws Exception {
assertEquals(48d,mTools.sum(46d,2d),0);
System.out.println("Test - testSum");
}
@Test
public void testAbs() throws Exception {
assertEquals(46,mTools.abs(-46),0);
System.out.println("Test - testAbs");
}
}
實現(xiàn)Tools類
public class Tools {
public double sum(double a ,double b){
return a + b;
}
public double abs(double value){
return Math.abs(value);
}
}
Run Test Unit
右鍵ToolsTest類德挣,選擇Run->ToolsTest
測試結(jié)果:
補充
Generate選擇setUp/@Before和tearDown/@After
其運行順序是setUp->測試的方法->tearDown(即Before->Test->After,且每個Test方法都會去執(zhí)行一次setUp和tearDown方法)
測試結(jié)果:
使用Espresso進行Instrumentaion Test
在build.gradle中配置Espresso,在defaultConfig中添加
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
在dependencies中添加
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
完整的內(nèi)容
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "me.peace.espressostudy"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0-beta1'
androidTestCompile 'com.android.support:support-annotations:24.0.0-beta1'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
編輯layout的布局文件,效果如圖
在MainActivity中加入如下代碼
//按鈕點擊事件處理
public void print(View v){
TextView textView = (TextView)findViewById(R.id.textview);
EditText editText = (EditText)findViewById(R.id.edittext);
textView.setText("message => " + editText.getText().toString().trim());
}
在androidTest/java目錄下創(chuàng)建MainActivityTest并且添加如下代碼
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest{
private String mExpectedString = "Hello";
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
@Test
public void print(){
onView(withId(R.id.edittext)).perform(typeText(mExpectedString),closeSoftKeyboard());
onView(withText(R.string.print)).perform(click());
onView(withId(R.id.textview)).check(matches(withText("message => " + mExpectedString)));
}
}
選擇MainActivityTest快毛,右鍵Run 'MainActivityTest'
手機屏幕就會看到被執(zhí)行的動作格嗅,最后會看到as上輸出測試結(jié)果。
https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html