在Android SDK中“Resources”-“Tutorials”下有“Notepad Tutorial”和“Activity Testing”两个项目,一个示例是指导你如何快速开发一个Android小程序,一个是指导你如何对项目进行测试,两个项目都适合在入门的时候好好学习。

其中的“Activity Testing”是对“Samples”-“Spinner”项目进行测试,其中包含了UI测试、状态破坏和状态恢复测试。这个项目只有一个Activity,测试起来也不麻烦,细心阅读文档就可以完成。但是一个程序只有一个Activity应该是很难遇见的吧,那么应该对多活动(Multi Activities)的程序进行测试呢?
其实我这也是随便整整,大家随便看看。

在查看SDK关于测试的章节后,有疑问如下:
测试Activity、Service、Provider都是自动化的,那么我们如何控制运行过程?
如何在界面模拟操作,如点击按钮,输入文字内容等等。


新建一个项目,项目名为Login,包名为com.vruc.android.login,程序名为Login,活动名为AuthenticateActivity;同时添加一个项目名为LoginTest,包名为com.vruc.android.login.test,程序名为LoginTest的测试项目。

完整的Login项目:
1.更改main.xml文件名为login.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"><EditText android:id="@+id/username_field"android:layout_height="wrap_content" android:layout_width="match_parent"></EditText><EditText android:id="@+id/password_field"android:layout_height="wrap_content" android:layout_width="match_parent"></EditText><Button android:text="Login" android:id="@+id/login_button"android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>

2.打开AuthenticateActivity.java文件,为“@+id/login_button”添加点击事件,具体代码就是向WelcomeActivity传递当前“@+id/username_field”中的输入文字并结束当前activity,具体代码为下:
public class AuthenticateActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login);Button login = (Button) findViewById(R.id.login_button);login.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent i = new Intent(AuthenticateActivity.this,WelcomeActivity.class);i.putExtra(ACCOUNT_SERVICE,((EditText) findViewById(R.id.username_field)).getText().toString());startActivity(i);finish();}});}}

3.在layout目录下添加新文件welcome.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:id="@+id/welcome_message"android:layout_width="wrap_content" android:layout_height="wrap_content"android:textSize="15pt"></TextView></LinearLayout>

4.添加新的WelcomeActivity.java文件并在AndroidMainifest.xml中注册,重写onCreate事件,具体代码就是为“@+id/welcome_message”赋值,从LoginActivity中传递过来的“@+id/username_field”的值,具体代码为下:
public class WelcomeActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.welcome);Intent i = this.getIntent();((TextView)findViewById(R.id.welcome_message)).setText(i.getStringExtra(ACCOUNT_SERVICE));}


现在可以运行一下Login项目,可以发现填写在“@+id/username_field”中的文字在点击“@+id/login_button”按钮后出现在了WelcomeActivity中。

完整的LoginTest项目
1.添加LoginTest.java文件,继承类为android.test.InstrumentationTestCase
2.完整LoginTest.java中测试代码:
public static final String TEST_USERNAME = "TEST_USERNAME";public static final String TEST_PASSWORD = "TEST_PASSWORD";public void testUserLogin() {// 注册最开始的活动并运行Instrumentation instrumentation = getInstrumentation();ActivityMonitor monitor = instrumentation.addMonitor(AuthenticateActivity.class.getName(), null, false);// 运行活动Intent intent = new Intent(Intent.ACTION_MAIN);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setClassName(instrumentation.getTargetContext(), AuthenticateActivity.class.getName());instrumentation.startActivitySync(intent);// 等待Authenticate活动开始Activity currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);assertTrue(currentActivity != null);// 自动输入预定的用户名View currentView = currentActivity.findViewById(com.vruc.android.login.R.id.username_field);assertTrue(currentView != null);TouchUtils.clickView(this, currentView);instrumentation.sendStringSync(TEST_USERNAME);// 自动输入预定的密码currentView = currentActivity.findViewById(com.vruc.android.login.R.id.password_field);assertTrue(currentView != null);TouchUtils.clickView(this, currentView);instrumentation.sendStringSync(TEST_PASSWORD);// 移除当前活动监视,注册新的活动监视,要在还没有按下按钮前准备instrumentation.removeMonitor(monitor);monitor = instrumentation.addMonitor(WelcomeActivity.class.getName(), null, false);// 自动点击登陆按钮currentView = currentActivity.findViewById(com.vruc.android.login.R.id.login_button);assertTrue(currentView != null);TouchUtils.clickView(this, currentView);// 等待Welcome活动开始currentActivity = getInstrumentation().waitForMonitorWithTimeout(monitor, 5);currentView = currentActivity.findViewById(com.vruc.android.login.R.id.welcome_message);assertTrue(currentView != null);assertEquals(TEST_USERNAME, ((TextView) currentView).getText().toString());}


运行测试程序后可以发现testUserLogin顺利通过,也可以在模拟器查看具体的运行过程,就像你新手操作模拟器一般。

接下来解释下“Instrumentation”类,未完待续。。。。。。。。


更多相关文章

  1. android测试工具大全
  2. android 用命令行打包生成 apk
  3. android uiautomator自动化测试
  4. 为你的Android实现测试覆盖率
  5. 移动周刊第 199 期:Android(安卓)性能小技巧、iOS 项目及性能优化
  6. 为你的Android实现测试覆盖率
  7. Android网络收音机项目
  8. Android热补丁动态修复技术(完结篇):自动生成打包带签名的补丁,重
  9. Android工程下build target,minSdkVersion,targetSdkVersion,maxSdk

随机推荐

  1. Android——属性动画(Property Animation)
  2. Android(安卓)使用模拟位置(支持Android(
  3. android 编译之后的文件系统和内核
  4. 编译FFMpeg的Android版本,并整合到android
  5. Android(安卓)init.rc BOOTCLASSPATH
  6. Android(安卓)SharedPreferences保存登录
  7. Android(安卓)用style简化layout布局文件
  8. android延迟执行任务(刷新按钮旋转)
  9. android 访问网络不能在主线程中进行以及
  10. 安装和卸载Android应用程序(apk包)