Espresso(-第一个项目)

本文针对Android studio

一、创建一个项目

       像平常一样直接创建一个android项目即可。

1、在app/build.gradle文件中如下配置

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultConfig {
        applicationId "com.collection.self.com.espressotest"
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        //-----Espresso中要求添加的部分(必选项)
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
        exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
//    testCompile 'junit:junit:4.12'----这个一定要删除,否则AndroidJUnit4无法下载下来
    androidTestCompile 'com.android.support:support-annotations:23.1.1'
    compile 'com.android.support:appcompat-v7:23.1.1'
// Android JUnit Runner-----Espresso中要求添加的部分(必选项)
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    // JUnit4 Rules-----Espresso中要求添加的部分(必选项)
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    // Espresso core-----Espresso中要求添加的部分(必选项)
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
//    // Espresso-contrib for DatePicker, RecyclerView, Drawer actions, Accessibility checks, CountingIdlingResource
//    //-----Espresso中要求添加的部分(可选项)
//    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
//    // Espresso-web for WebView support-----Espresso中要求添加的部分(可选项)
//    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.1'
//    // Espresso-idling-resource for synchronization with background jobs-----Espresso中要求添加的部分(可选项)
//    androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:2.2.1'
}

 

2、MainActivity代码如下

 package com.collection.self.com.espressotest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sayHello(View view){
        TextView textView = (TextView)findViewById(R.id.textView);
        EditText editText = (EditText)findViewById(R.id.editText);
        textView.setText("Hello,"+editText.getText().toString()+"!");
    }
}

 

3、activity_main.xml文件配置如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.collection.self.com.espressotest.MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name here"
        android:id="@+id/editText"
        android:layout_below="@+id/textView"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Say hello!"
        android:layout_below="@+id/editText"
        android:onClick="sayHello"/>
</RelativeLayout>

 

4、测试是否可以正确运行

    点击运行按钮测试程序是否可以正常运行

Espresso(-第一个项目)_第1张图片

二、添加Espresso的测试代码

1、目前的文件结构如下:

Espresso(-第一个项目)_第2张图片

2、新建对应的测试文件:

在app/src/androidTest/项目对应的packageName/此目录中新建一个类。目前用到的类的名字为:MainActivityInstrumentationTest.java

其中具体的代码如下:

 package com.collection.self.com.espressotest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
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;
/**
 * Created by jane on 2016/2/19.
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityInstrumentationTest {
    private static final String STRING_TO_BE_TYPED = "Peter";
    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);
    @Test
    public void sayHello(){
        onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); //line 1
        onView(withText("Say hello!")).perform(click()); //line 2
        String expectedText = "Hello, " + STRING_TO_BE_TYPED + "!";
        onView(withId(R.id.textView)).check(matches(withText(expectedText))); //line 3
    }
}

3、运行测试文件

a、对于测试机的设置

关闭测试机三个选项。具体操作步骤如下:

On your device, under Settings->Developer options disable the following 3 settings:

  • Window animation scale

  • Transition animation scale

  • Animator duration scale

b、运行测试文件

在MainActivityInstrumentationTest.java中右键点击,在弹出的菜单中选择Run MainActivityInstrume。

如下图所示:

Espresso(-第一个项目)_第3张图片

c、测试结果

Espresso(-第一个项目)_第4张图片

三、遇到的问题解决方法

 【报错内容】

java.lang.NoClassDefFoundError: com.collection.self.com.espressotest.MainActivity
at com.collection.self.com.espressotest.MainActivityInstrumentationTest.<init>(MainActivityInstrumentationTest.java:41)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1667)

 

 【解决方案】

        主要原因是 app/build.gradle配置文件中有多余的配置选项导致的。所以只需要将不需要的Espresso配置文件删除掉就可以了。

        最后的结果见上面的build.gradle的配置文件

 

 

 

参考地址:

http://www.jianshu.com/p/03118c11c199

你可能感兴趣的:(Espresso(-第一个项目))