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());
    }
}

 

你可能感兴趣的:(eclipse,xml,android,ant,JUnit)