Android单元测试
创建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'
}
创建Unit Test
创建测试类Tools
public class Tools {
public double sum(double a ,double b){
return 0;
}
public double abs(double value){
return 0;
}
}
在待测试的类(Tools)代码编辑器内,右键选择Go to -> Test
或者使用快捷键Ctrl + Shift + T,Create New Test 。
Testing library选择JUnit4
Generate选择setUp/@Before
Destionation package选择生成的测试类的包路径
如下图:
创建成功后会在app/src/test/java/me/peace/junit下生成测试类(me/peace/junit即Destionation package)
一般与javas相关的单元测试选择test/java目录,与android相关的选择androidTest/java,若缺少这两个目录请自行创建
在这个测试类中加入具体的测试内容
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");
}
}
实现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
测试结果:
补充
Generate选择setUp/@Before和tearDown/@After
其运行顺序是setUp->测试的方法->tearDown(即Before->Test->After,且每个Test方法都会去执行一次setUp和tearDown方法)
测试结果:
使用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'
完整的内容
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目录下创建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 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'
手机屏幕就会看到被执行的动作,最后会看到as上输出测试结果。
https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html
Addition
https://github.com/peace710/AgeraCore