资讯专栏INFORMATION COLUMN

使用 Android Studio 进行测试 (二) UI 测试

sshe / 1172人阅读

摘要:编码为添加交互编写测试在包中新建测试类进行测试测试文件上点右键测试就可以了。稍后会看到手机自动运行,并按照测试代码中写的进行自动测试。

目录

单元测试

UI 测试

原文链接: Unit and UI Testing in Android Studio

2. UI 测试

配置

编码

测试

2.1 配置

2.1.1 IDE 配置
Build Variants => Test Artifact => Android Instrumentation Tests

2.1.2 build.gradle

apply plugin: "com.android.application"

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.testing.testingexample"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"

        //ADD THIS LINE:
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    //ADD THESE LINES:
    packagingOptions {
        exclude "LICENSE.txt"
    }
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:22.0.0" //← MAKE SURE IT’S 22.0.0
    testCompile "junit:junit:4.12"

    //ADD THESE LINES:
    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"
}

重要:由于一些依赖版本冲突,你需要确认com.android.support:appcompat-v7库的版本号是22.0.0,像上面的代码片段一样。

2.2 编码

2.2.1 为 app 添加交互

activity_main.xml



    
    
    

MainActivity.java

public void sayHello(View v){
    TextView textView = (TextView) findViewById(R.id.textView);
    EditText editText = (EditText) findViewById(R.id.editText);
    textView.setText("Hello, " + editText.getText().toString() + "!");
}

2.2.2 编写测试

androidTest 包中新建测试类
MainActivityInstrumentationTest.java

import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.action.ViewActions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.After;
import org.junit.Before;
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;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityInstrumentationTest {

    private static final String STRING_TO_BE_TYPED = "Peter";

    @Rule
    public ActivityTestRule 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
    }

}
2.3 进行测试

测试文件上点右键 Run 测试就可以了。
稍后会看到手机自动运行 app,并按照测试代码中写的进行自动测试。

2.4 测试结果

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/8705.html

相关文章

  • 使用 Android Studio 进行测试 () UI 测试

    摘要:编码为添加交互编写测试在包中新建测试类进行测试测试文件上点右键测试就可以了。稍后会看到手机自动运行,并按照测试代码中写的进行自动测试。 目录 单元测试 UI 测试 原文链接: Unit and UI Testing in Android Studio 2. UI 测试 配置 编码 测试 2.1 配置 2.1.1 IDE 配置 Build Variants => Test Ar...

    kidsamong 评论0 收藏0
  • UI自动化测试】Mac下进行Monkey测试

    摘要:一对压力测试的简介是中的一命令行工具,对的稳定性和健壮性进行测试。完成以上操作后就可进行测试了,每次只能对一台设备进行测试。 【写在前面】在进行app测试中了解到monkey测试,故在网上学习了一些资料,但自己在实践中仍有一些疑惑和问题出现。在此记录下自己的实际操作,后续仍然会不断调整,以期完善。 一、对monkey压力测试的简介 monkey:是Android中的一命令行工具,对ap...

    shinezejian 评论0 收藏0
  • fir.im Weekly - 从零开始创建 Android 新项目

    摘要:链接在此新浪微博客户端架构与优化之路来自移动开发前线的分享。沙龙名额有限,想参加的同学快来报名吧网页链接安卓上款最佳黑客工具由黑客与极客微博分享。 今年的 Google I/O 大会上,人工智能和虚拟现实的产品发布让我们对未来多了几分惊喜。对于开发者部分,Google 发布了 Android N 系统,感受最深的是全新的 Android Studio 、 Firebase,赋予开发者更...

    zqhxuyuan 评论0 收藏0
  • fir.im Weekly - 从零开始创建 Android 新项目

    摘要:链接在此新浪微博客户端架构与优化之路来自移动开发前线的分享。沙龙名额有限,想参加的同学快来报名吧网页链接安卓上款最佳黑客工具由黑客与极客微博分享。 今年的 Google I/O 大会上,人工智能和虚拟现实的产品发布让我们对未来多了几分惊喜。对于开发者部分,Google 发布了 Android N 系统,感受最深的是全新的 Android Studio 、 Firebase,赋予开发者更...

    hoohack 评论0 收藏0
  • 使用 Android Studio 进行测试 (一) 单元测试

    摘要:目录单元测试测试原文链接单元测试配置编码测试配置配置编码被测类测试类在目录建立目录在目录下自动生成类测试右键点击类,选择。也可以通过命令行运行测试,在工程目录内输入测试结果由于没有对进行具体实现,测试全部失败。实现后重新测试即可通过。 目录 单元测试 UI 测试 原文链接: Unit and UI Testing in Android Studio 1 单元测试 配置 编码 测试...

    wyk1184 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<