我没有得到我错的地方。使用android studio并尝试编写测试用例。我创建了一个新项目,结构是:
-app
-src
-androidTests
-java (com.example.testdemo.tests)
- MainActivityTest
-main
-java (com.example.testdemo)
-MainActivity
-res
-AndroidManifest
我将一步一步地走下去:
1. build.gradle 文件内容
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.testdemo"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
testApplicationId "com.example.testdemo.tests"
}
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:23.0.1'
}
MainActivityTest.java
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.TextView;
import com.example.webonise.testdemo.MainActivity;
import com.example.webonise.testdemo.R;
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private TextView tv;
private MainActivity mActivity;
public MainActivityTest(Class<MainActivity> activityClass) {
super(activityClass);
}
@Override
public void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
tv = (TextView)mActivity.findViewById(R.id.tv);
assertNotNull("Text view is null",tv);
}
public void testPreconditions(){
assertNotNull("Text view is null",tv);
}
@SmallTest
public void checkTextViewText(){
String text = tv.getText().toString();
assertEquals(mActivity.getResources().getString(R.string.hello_world), text);
}
@SmallTest
public void checkTextViewText2(){
String text = tv.getText().toString();
assertEquals("fail", text);
}
}
在工作室终端我给出了这个命令: ./ gradlew connectedCheck
我的建立成功但是生成的报告显示了这一点:
测试摘要 0 测试 0
成功 类 班级测试失败持续时间成功率
我感谢你的帮助。