Debug测试(2017年9月26日)

1、如何使用Step into进行程序调试?

编写程序:

activity_main.xml

 

MainActivity.java

 

 

debug开始:

 

 

2、程序调试——使用Toast

 

3、程序调试(四)——单元测试

步骤:

1)为项目设置依赖——junit(单元测试jar包)

Android Studio中找到app下的build.gradle:

其中包含了dependencies节点,表示当前模块依赖哪些jar包。在内部有compiletestcompile表示编译依赖和测试编译依赖。

 

2)使用junit

在当前包下找到test包,创建一个单元测试类。

为单元测试类中测试方法添加@Test注解。

在测试方法中使用assertEquals(参数1,参数2)方法,比较期望值与实际值是否相符。如果相符表示测试通过,如果不符,表示测试有错。

项目结构:

 

MyUtil.java:

 

MainActivity.java

 

MyJunitTest.java

 

执行Test左侧的run按钮:

 

4、程序调试(五)——UI测试

Instrumentation测试

依赖(App下的build.gradle):

 

项目结构:

 

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:id="@+id/activity_main"

    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.example.administrator.myapplication.MainActivity">

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!"

        android:id="@+id/textView" />

 

    <EditText

        android:hint="Enter your name here"

        android:id="@+id/editText"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        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>

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private Button button = null;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

    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() + "!");

    }

 

 

 

}

MainActivityInstrumentationTest.java:

import android.support.test.filters.LargeTest;

import android.support.test.rule.ActivityTestRule;

import android.support.test.runner.AndroidJUnit4;

 

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

    }

 

 

}

测试效果(测试框架会自动模拟人对UI进行操作):

 

调试分类:

1debug

2logcat

3toast

4junit

5ui test

5UI设计中常用的控件

1)文本框——TextView

 

效果:

 

【案例】理解gravitylayout_gravity

 

【案例】使用TextView设置链接

 

 

【案例】设置TextView文字的大小和颜色、单行显示

 

 

作业:

制作一个个人介绍,要求:

1)包含个人照片

2)文字作相关介绍

3)通过改变颜色,字号,单行显示,对齐方式等美化界面

 

posted @ 2021-03-25 13:49  三生石头花  阅读(72)  评论(0编辑  收藏  举报