在本教程中,您将学习如何使用Eclipse创建Android JUnit测试项目,创建自动化单元测试并在各种条件下运行它们。
Android基础
能使用Eclipse和 Android SDK之类的工具。
代码下载:https://simple-calc-unit-testing.googlecode.com/files/FullCodeDownload.zip
屏幕具有两个简单的功能:它允许用户输入的两个值,求和或者积。
屏幕布局如下main.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" android:id="@+id/mainLayout"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:gravity="center_horizontal" android:textSize="48px" android:padding="12px" /> <EditText android:layout_height="wrap_content" android:id="@+id/value1" android:hint="@string/hint1" android:inputType="numberDecimal" android:layout_width="fill_parent" android:textSize="48px"></EditText> <EditText android:layout_height="wrap_content" android:id="@+id/value2" android:hint="@string/hint2" android:inputType="numberDecimal" android:layout_width="fill_parent" android:textSize="48px"></EditText> <FrameLayout android:id="@+id/FrameLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="12px" android:background="#ff0000"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#000000" android:padding="4px"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/resultLabel" android:textSize="48px" android:id="@+id/resultLabel"></TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/result" android:textSize="48px" android:textStyle="bold" android:layout_marginLeft="16px"></TextView> </LinearLayout> </FrameLayout> <LinearLayout android:id="@+id/LinearLayout03" android:layout_height="wrap_content" android:layout_width="fill_parent"> <Button android:layout_height="wrap_content" android:id="@+id/addValues" android:text="@string/add" android:textSize="32px" android:layout_width="wrap_content"></Button> <Button android:layout_height="wrap_content" android:id="@+id/multiplyValues" android:text="@string/multiply" android:textSize="32px" android:layout_width="wrap_content"></Button> </LinearLayout> </LinearLayout>
代码MainActivity.java:
/* * Copyright (c) 2010, Lauren Darcey and Shane Conder * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used * to endorse or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * <ORGANIZATION> = Mamlambo */ package com.mamlambo.article.simplecalc; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { final String LOG_TAG = "MainScreen"; super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText value1 = (EditText) findViewById(R.id.value1); final EditText value2 = (EditText) findViewById(R.id.value2); final TextView result = (TextView) findViewById(R.id.result); Button addButton = (Button) findViewById(R.id.addValues); addButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { try { int val1 = Integer.parseInt(value1.getText().toString()); int val2 = Integer.parseInt(value2.getText().toString()); Integer answer = val1 + val2; result.setText(answer.toString()); } catch (Exception e) { Log.e(LOG_TAG, "Failed to add numbers", e); } } }); Button multiplyButton = (Button) findViewById(R.id.multiplyValues); multiplyButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { try { int val1 = Integer.parseInt(value1.getText().toString()); int val2 = Integer.parseInt(value2.getText().toString()); Integer answer = val1 * val2; result.setText(answer.toString()); } catch (Exception e) { Log.e(LOG_TAG, "Failed to multiply numbers", e); } } }); } }
您可以通过两种方式添加一个测试项目,添加Android测试项目或从Android Tools添加项目。我们演示后者。
在项目资源管理器中,右键单击要测试的项目,选择Android Tools->New Test Project。
生成的代码如下:
package com.mamlambo.article.simplecalc.test; import android.test.ActivityInstrumentationTestCase2; public class MathValidation extends ActivityInstrumentationTestCase2<MainActivity> { public MathValidation(String name) { super(name); } protected void setUp() throws Exception { super.setUp(); } }
注意:忽视“Superclass does not exist.”告警即可。
在构建方法关联被测应用:
public MathValidation() { super("com.mamlambo.article.simplecalc", MainActivity.class); }
初始化
package com.mamlambo.article.simplecalc.test; import android.test.ActivityInstrumentationTestCase2; import android.widget.TextView; import com.mamlambo.article.simplecalc.MainActivity; import com.mamlambo.article.simplecalc.R; public class MathValidation extends ActivityInstrumentationTestCase2<MainActivity> { private TextView result; public MathValidation() { super ("com.mamlambo.article.simplecalc", MainActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); MainActivity mainActivity = getActivity(); result = (TextView) mainActivity.findViewById(R.id.result); } }
第一个测试用例
private static final String NUMBER_24 = "2 4 ENTER "; private static final String NUMBER_74 = "7 4 ENTER "; private static final String ADD_RESULT = "98"; public void testAddValues() { sendKeys(NUMBER_24); // now on value2 entry sendKeys(NUMBER_74); // now on Add button sendKeys("ENTER"); // get result String mathResult = result.getText().toString(); assertTrue("Add result should be 98", mathResult.equals(ADD_RESULT)); }
执行测试
检查结果:
英文原版:http://code.tutsplus.com/tutorials/android-sdk-unit-testing-with-the-junit-testing-framework--mobile-421
原文:http://www.cnblogs.com/pythontesting/p/4914955.html