使用Android Studio进行UI测试与Record Espresso Test自动生成测试代码

一、使用Android Studio 进行UI测试


1.新建项目TestExample,创建后可运行。

2.配置 build.gradle

apply plugin: 'com.android.application'  
  
android {  
    compileSdkVersion 25  
    buildToolsVersion "25.0.3"  
    defaultConfig {  
        applicationId "com.example.testexample"  
        minSdkVersion 25  
        targetSdkVersion 25  
        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'])  
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {  
        exclude group: 'com.android.support', module: 'support-annotations'  
    })  
    compile 'com.android.support:appcompat-v7:25.3.1'  
    compile 'com.android.support.constraint:constraint-layout:1.0.2'  
    testCompile 'junit:junit:4.12'  
  
    androidTestCompile 'com.android.support.test:runner:0.2'  
    androidTestCompile 'com.android.support.test:rules:0.2'  
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'  
    
}  
首先是defaultConfig中添加(如未自动生成。)testInstrumentationRunner  "android.support.test.runner.AndroidJUnitRunner"

 
 
其次是dependencies里的
 androidTestCompile 'com.android.support.test:runner:0.2'  
 androidTestCompile 'com.android.support.test:rules:0.2'  
 androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'  

 
 
如果编译出现下面问题 Error:Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (25.3.1) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
 
 
解决方法有两种:
 
 

第一种:把appcompat-v7:25.3.1改为appcompat-v7:23.1.1

第二种 :添加 androidTestCompile 'com.android.support:support-annotations:25.3.1'

3.给app添加功能

在这里我们添加功能,就是登陆

4.编写测试

(1)创建测试类可以直接在androidTest包中直接创建类LoginActivityTest,也可以

选择Create New Test...


这样就创建了一个测试类。

(2)编写测试用例,登录测试如下

@LargeTest  
@RunWith(AndroidJUnit4.class)  
public class LoginActivityTest {  
    private static final String USER_EMAIL = "[email protected]";  
    private static final String USER_PASSWORD = "11111111";  
  
    @Rule  
    public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<>(LoginActivity.class);  
  
    @Test  
    public void onLoginTest() {  
        //不输入任何内容  
        onView(withId(R.id.userNameEt)).perform(clearText());
        onView(withId(R.id.passWordEt)).perform(clearText());  
        onView(withId(R.id.loginBtn)).perform(click());  
        try {  
            Thread.sleep(500);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        onView(withId(R.id.dialog_cancel)).perform(click());  
          
        //输入用户名密码  
        onView(withId(R.id.userNameEt)).perform(typeText(USER_EMAIL), closeSoftKeyboard());  
        onView(withId(R.id.passWordEt)).perform(typeText(USER_PASSWORD), closeSoftKeyboard());  
        onView(withId(R.id.loginBtn)).perform(click());  
        try {  
            Thread.sleep(500);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
    }  
}

其中:

onView(withId(R.id.userNameEt)):找到对应Id的View

perform(click()):对找到的这个View进行点击操作,还有其他操作比如:clearText:清除


二、使用Record Espresso Test自动生产测试代码

运行Run ->Record Espresso Test,然后就在测试机上跑,会根据你的操作会自动生成测试代码。


猜你喜欢

转载自blog.csdn.net/qq_33333570/article/details/77318725