成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

使用 Android Studio 進(jìn)行測試 (二) UI 測試

sshe / 1412人閱讀

摘要:編碼為添加交互編寫測試在包中新建測試類進(jìn)行測試測試文件上點(diǎn)右鍵測試就可以了。稍后會(huì)看到手機(jī)自動(dòng)運(yùn)行,并按照測試代碼中寫的進(jìn)行自動(dòng)測試。

目錄

單元測試

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"
}

重要:由于一些依賴版本沖突,你需要確認(rèn)com.android.support:appcompat-v7庫的版本號(hào)是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 進(jìn)行測試

測試文件上點(diǎn)右鍵 Run 測試就可以了。
稍后會(huì)看到手機(jī)自動(dòng)運(yùn)行 app,并按照測試代碼中寫的進(jìn)行自動(dòng)測試。

2.4 測試結(jié)果

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/8705.html

相關(guān)文章

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<