`
huakewoniu
  • 浏览: 47635 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

android 测试初探(android test)

阅读更多

android
Testing and Instrumentation
Key features of the Android testing environment include:

Android extensions to the JUnit framework that provide access to Android system objects.
An instrumentation framework that lets tests control and examine the application.
Mock versions of commonly-used Android system objects.
Tools for running single tests or test suites, with or without instrumentation.
Support for managing tests and test projects in the ADT Plugin for Eclipse and at the command line

下面实现一个简单的测试程序、
你的负责测试的程序是通过在其manifest 文件中<instrumentation> 标签来把待测试的程序连接到一起
The attributes of the element specify the package name of the application under test and also tell
<instrumentation> 的属性确定了待测试的程序的包名,并告诉Android 如何去运行这个测试程序 Instrumentation 更详细
的描述了Instrumentation Test Runner.


 Eclipse with ADT provides an extensive set of tools for creating tests, running them, and viewing their results. You can also use the adb tool to run tests, or use a built-in Ant target.
在android中测试程序和android 程序的写法是一样的。SDK tools 帮助你同时创建 main application project and its test project。你可以在eclipse中用ADT 或是
用命令行还完成测试。你可以参考Testing in Eclipse 去学习怎样在eclipse中编写和运行测试程序
接下来的Hello, Testing tutorial 将会介绍一些基本的测试概念

在这个例子中你将会学习android程序开发的基本原理。这个例子将会引导你用ADTsetup一个android的测试项目。
Creating the Test Project
In the Hello World tutorial you created Android application project called HelloAndroid. A test of an Android application is
 also an Android application, and you create it within an Eclipse project.
  The Eclipse with ADT New Android Test Project
 dialog creates a new test project and the framework of a new test application at the same time.

待测程序源代码
the activity:
public class HelloTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
下面是maim.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>
string.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World!</string>
    <string name="app_name">HelloAndroidTest</string>
</resources>


按如下步骤创建一个test project
In Eclipse, select New > Project > Android > Android Test Project.
 

The New Android Test Project dialog appears.

Set the following values:
Test Project Name: "HelloAndroidTest"
Test Target: Set "An existing Android project", click Browse, and then select "HelloAndroid" from the list of projects.
Build Target: Set a target whose platform is Android 1.5 or above.
Application name: "HelloAndroidTest"

Click Finish. The new project appears in the Package Explorer.

然后Creating the Test Case Class
 The basic structure includes all the files and directories you need to build and run a test application, except for the class
 that contains your tests (the test case class).
现在你有一个test project 叫HelloAndroidTest 这个项目中最基本的结构都已经帮你创建好了,你只需要自己创建一个Test Case Class, 这个类当中
将会包含你想要做的测试
The next step is to define the test case class. In this tutorial, you define a test case class that extends one of
Android's test case classes designed for Activities. The class contains definitions for four methods:

HelloAndroidTest: This defines the constructor for the class. It is required by the Android testing framework.
setUp(): This overrides the JUnit setUp() method. You use it to initialize the environment before each test runs.
testPreconditions(): This defines a small test that ensures the Hello, Android application starts up correctly.
testText(): This tests that what is displayed on the screen is the same as what is contained in the application's
string resources. It is an example of a real unit test you would perform against an application's UI.

下面这个test case 是测试界面上显示的 android:text="@string/hello"的内容是否和string.xml资源文件中的内容一样
public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> {


    private HelloAndroid mActivity;
    private TextView mView;
    private String resString;
    /**
     * @param pkg
     * @param activityClass
     */
    public HelloAndroidTest() {
        super("hust.ophoneclub.HelloAndroid",HelloAndroid.class );
        // TODO Auto-generated constructor stub
    }
   
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // initialize variables and prepare the test environment.
        mActivity = this.getActivity();
        mView = (TextView)mActivity.findViewById(hust.ophoneclub.HelloAndroid.R.id.textview);
       resString = mActivity.getString(hust.ophoneclub.HelloAndroid.R.string.hello);
    }
   
    public void testPreconditions() {
        //A preconditions test checks the initial application conditions prior to executing other tests
        assertNotNull(mView);
    }
   
    public void testText() {
        assertEquals(resString, (String) mView.getText());
    }
}

 

分享到:
评论

相关推荐

    Android单元测试初探——Instrumentation

    学习Android有一段时间了,虽然前段时间对软件测试有了一些了解,不过接触android的单元测试却是头一次。这几天在物流大赛上也用了不少时间,所以对于android的单元测试没有太深入的研究,所以先写个基本入门吧!...

    Android自动化测试初探.doc

    【Android自动化测试初探】 Android自动化测试是一种针对Android应用程序进行的自动化的质量验证过程,它旨在减少手动测试的重复劳动,提高测试效率和覆盖率。在Android系统中,自动化测试通常涉及UI测试、功能测试...

    Android自动化测试初探

    首先,我们了解到传统的Android自动化测试通常依赖于JUnit框架和Android SDK中的`android.test`包,但这需要应用程序的源代码,这对于许多只做黑盒测试的测试工程师来说并不实际。 Android系统中的权限控制严格限制...

    android自动化测试资料

    3.Create_Android_Sample_...Android自动化测试初探 Android自动化测试可行性途径分析 Android自动化测试之道 GSM手机自动化测试解决方案 手机自动化测试系统设计浅析 在Android_Robotium自动化测试中导出Test_result

    android 自动化测试

    Android 自动化测试初探 Android 自动化测试是一个新的领域,网上关于这方面的资料很多都是基于白盒测试的,一般都是基于 JUnit 框架和 Android SDK 中 android.test 等命名空间下的内容进行。然而,在很多软件...

    android自动化

    ### Android自动化测试初探 #### 一、前言与背景 随着移动互联网的快速发展,Android作为全球最大的移动操作系统之一,在其应用开发过程中面临着各种各样的挑战。为了确保应用的质量,测试变得尤为重要。传统上,...

    Android 开发技巧

    9.86、ANDROID自动化测试初探 324 9.86.1、捕获Activity上的Element 324 9.86.2、Hierarchyviewer 捕获Element的 328 9.86.3、架构实现 330 9.86.4、模拟键盘鼠标事件(Socket+Instrumentation实现) 332 9.86.5、再...

    gRPC框架初探

    ### gRPC框架初探 #### 一、gRPC框架简介 gRPC是一个高效的远程过程调用(Remote Procedure Call,简称RPC)框架,由Google开发并开源。它支持多种编程语言,如C++、Java、Python、Go等,并利用HTTP/2进行传输层...

    安卓java读取网页源码-android_test_iot_for_tecent:将安卓设备作为物联网设备接入腾讯云IOT平台

    安卓开发初探 目的:将Andorid端作为一个物联网设备(device),然后将其安卓设备上面的数据发送到腾讯云IOT开发平台上。(这里我们将手机上面的GPS经纬度发送到腾讯云IOT平台上)。 腾讯IOT开发平台: 腾讯IOT Java...

    hkcc-pacman-robot:CC 学生版 Pacman CCN3140 编程项目 香港理工大学-香港社区学院

    hkcc-pacman-robotCC 学生版吃...实现安卓应用 实施服务器 构建机器人抽象动作 API 实现 Java 应用程序 结合 edu.hkcc.pacmanrobot.launcher.Test the Robot Program with Android App, Server 最终 edu.hkcc.pacman

Global site tag (gtag.js) - Google Analytics