本文是在上一篇文章《java单元测试》的基础上继续讲解android的单元测试,android源码中引入了java单元测试的框架(android源码目录:libcore\junit\src\main\java\junit\framework中可见),然后在java单元测试框架的基础上扩展属于android自己的测试框架。android具体框架类的关系图如下

从上图的类关系图中可以知道,通过android测试类可以实现对android中相关重要的组件进行测试(如Activity,Service,ContentProvider,甚至是application)。

 其实在android源码中,基本上每个系统应用都自带一个测试工程,如下图的源码中settings(设置)模块:

上图的tests文件夹中就是settings模块自带的单元测试工程,有兴趣的读者可自行去研读一下源代码。

 

eclipse下(当然,前提是要保证eclipse中相关的android环境已经搭建好)进行android单元测试:

 

1.Application的测试:

   新建一个android项目,在该android项目添加一个继承Application的类,代码如下:

  1. package com.phicomm.hu;
  2. import android.app.Application;
  3. public class FxAndroidApplication extends Application
  4. {
  5. @Override
  6. public void onCreate()
  7. {
  8. // TODO Auto-generated method stub
  9. super.onCreate();
  10. }
  11. @Override
  12. public void onTerminate()
  13. {
  14. // TODO Auto-generated method stub
  15. super.onTerminate();
  16. }
  17. public String getFavourite()
  18. {
  19. return "I Love Java";
  20. }
  21. }

Appication类创建好后,接着创建对应的测试工程:选中其所在的android工程—->鼠标右键—–>new—->Android Test Project—–>输入测试工程名—>next—–>选择被测试的目标android工程(此处为FxAndroidApplication所在的android工程)。这样,一个测试工程就创建完成了。

    通过eclipse创建自动生成的测试工程项目和android工程项目结构上没什么大的区别,主要是在AndroidManifest.xml中有变化,如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.phicomm.hu.test"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk android:minSdkVersion="10" />
  8.  
  9. <instrumentation
  10. android:name="android.test.InstrumentationTestRunner"
  11. android:targetPackage="com.phicomm.hu" />
  12.  
  13. <application
  14. android:icon="@drawable/ic_launcher"
  15. android:label="@string/app_name" >
  16. <uses-library android:name="android.test.runner" />
  17. </application>
  18.  
  19. </manifest>

在AndroidManifest.xml注册了相关的测试环境(这些是android独有的):<uses-library android:name=”android.test.runner” />实现使用相关的运行测试类库,<instrumentation />中的targetPackage为被测试类所在的包。

    接下来在测试工程中创建FxAndroidApplicationd的测试类,代码如下:

  1. package com.phicomm.hu.test;
  2. import com.phicomm.hu.FxAndroidApplication;
  3. import android.app.Application;
  4. import android.test.ApplicationTestCase;
  5. public class FxApplicationTest extends ApplicationTestCase<FxAndroidApplication>
  6. {
  7. private FxAndroidApplication AppTest;
  8. public FxApplicationTest()
  9. {
  10. //调用父类构造函数,且构造函中传递的参数为被测试的类
  11. super(FxAndroidApplication.class);
  12. }
  13. @Override
  14. protected void setUp() throws Exception
  15. {
  16. // TODO Auto-generated method stub
  17. super.setUp();
  18. //获取application之前必须调用的方法
  19. createApplication();
  20. //获取待测试的FxAndroidApplication
  21. AppTest = getApplication();
  22. }
  23. //测试FxAndroidApplication的getFavourite方法
  24. public void testGetFavourite()
  25. {
  26. /*验证预测值"I Love C++"是否等于实际值,
  27. 由于实际值为"I love Java",所以此处测试结果为Failure*/
  28. assertEquals("I Love C++", AppTest.getFavourite());
  29. }
  30. }

 测试类创建好后,就可以实现对FxAndroidApplicationd进行测试了。

  测试方法:

   启动android模拟器(也可以通过android手机)—–>运行android工程—–>在测试工程中选中测试类FxApplicationTest—->鼠标右键—>Run As—->Android Junit Test。这样,测试结果就可以在eclipse的Junit视图上显示了,如下图:

通过上图的测试结果可知,ApplicationTestCase测试类中有两个测试方法是默认进行测试的(testGetFavourite才是我们要测试的方法)。

   当然,还可以通过adb进行测试:连接android手机——>打开电脑命令窗口(开始–>运行—>输入cmd)—->在命令窗口输入adb shell—->am instrument -w com.phicomm.hu.test(测试用例所在的包名)/android.test.InstrumentationTestRunner。

 

2.Activity的测试:

 

   和上面application一样,先创建一个android工程,该工程中创建了两个activity,一个activity实现输入用户信息的登录界面,另一个acticity显示输入的用户信息。

   效果图如下:

   登录界面FxLoginActivity的代码如下:

  1. package com.phicomm.hu;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. public class FxLoginActivity extends Activity
  10. {
  11. private EditText userName;
  12. private EditText passWord;
  13. /** Called when the activity is first created. */
  14. @Override
  15. public void onCreate(Bundle savedInstanceState)
  16. {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. userName = (EditText)findViewById(R.id.name);
  20. passWord = (EditText)findViewById(R.id.psd);
  21. Button login = (Button)findViewById(R.id.login);
  22. Button reset = (Button)findViewById(R.id.reset);
  23. //监听登录按钮
  24. login.setOnClickListener(new OnClickListener() {
  25. @Override
  26. public void onClick(View v)
  27. {
  28. // TODO Auto-generated method stub
  29. Intent intent = new Intent(FxLoginActivity.this, FxResultActivity.class);
  30. //通过intent传递登录信息到ResultActivity的界面中显示
  31. intent.putExtra("userName", userName.getText().toString());
  32. intent.putExtra("passWord", passWord.getText().toString());
  33. //启动ResultActivity显示登录界面信息
  34. startActivity(intent);
  35. }
  36. });
  37. //监听重置按钮
  38. reset.setOnClickListener(new OnClickListener()
  39. {
  40. @Override
  41. public void onClick(View v)
  42. {
  43. // TODO Auto-generated method stub
  44. resetInput();
  45. }
  46. });
  47. }
  48. public void resetInput()
  49. {
  50. userName.setText("");
  51. passWord.setText("");
  52. }
  53. }

main.xml布局文件的代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6.  
  7. <EditText
  8. android:id="@+id/name"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:hint="@string/name"/>
  12. <EditText
  13. android:id="@+id/psd"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:hint="@string/psd"/>
  17. <LinearLayout
  18. android:orientation="horizontal"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. >
  22. <Button
  23. android:id="@+id/login"
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="1"
  27. android:text="@string/login"/>
  28. <Button
  29. android:id="@+id/reset"
  30. android:layout_width="fill_parent"
  31. android:layout_height="wrap_content"
  32. android:layout_weight="1"
  33. android:text="@string/reset"/>
  34. </LinearLayout>
  35.  
  36. </LinearLayout>

显示用户信息界面的FxResultActivity代码如下:

  1. package com.phicomm.hu;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. public class FxResultActivity extends Activity
  9. {
  10. private static final String TAG = "ResultActivity";
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState)
  13. {
  14. // TODO Auto-generated method stub
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.result);
  17. TextView result = (TextView)findViewById(R.id.result);
  18. //通过得到intent获取登录界面传来的信息
  19. Intent intent = getIntent();
  20. String userName = intent.getStringExtra("userName");
  21. String passWord = intent.getStringExtra("passWord");
  22. //将登录信息在页面中显示
  23. result.setText("用户名:" + userName + "\n" + "密码:" + passWord);
  24. }
  25. }

  以上的android工程创建好后,创建一个对应的测试工程:

  测试工程中对应的FxLoginActivity类的测试代码如下(详细的代码讲解见代码中的相关注释,这里不在累赘):

  1. package com.phicomm.hu.test;
  2. import android.app.Instrumentation;
  3. import android.test.ActivityInstrumentationTestCase2;
  4. import android.view.KeyEvent;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import com.phicomm.hu.FxLoginActivity;
  8. public class FxLoginActivityTest extends ActivityInstrumentationTestCase2<FxLoginActivity>
  9. {
  10. private Instrumentation mInstrumentation;
  11. private FxLoginActivity mLoginTest;
  12. private EditText userName;
  13. private EditText passWord;
  14. private Button login;
  15. private Button reset;
  16. public FxLoginActivityTest()
  17. {
  18. super(FxLoginActivity.class);
  19. }
  20. //重写setUp方法,在该方法中进行相关的初始化操作
  21. @Override
  22. protected void setUp() throws Exception
  23. {
  24. // TODO Auto-generated method stub
  25. super.setUp();
  26. /**这个程序中需要输入用户信息和密码,也就是说需要发送key事件,
  27. * 所以,必须在调用getActivity之前,调用下面的方法来关闭
  28. * touch模式,否则key事件会被忽略
  29. */
  30. //关闭touch模式
  31. setActivityInitialTouchMode(false);
  32. mInstrumentation = getInstrumentation();
  33. //获取被测试的FxLoginActivity
  34. mLoginTest = getActivity();
  35. //获取FxLoginActivity相关的UI组件
  36. userName = (EditText)mLoginTest.findViewById(com.phicomm.hu.R.id.name);
  37. passWord = (EditText)mLoginTest.findViewById(com.phicomm.hu.R.id.psd);
  38. login = (Button)mLoginTest.findViewById(com.phicomm.hu.R.id.login);
  39. reset = (Button)mLoginTest.findViewById(com.phicomm.hu.R.id.reset);
  40. }
  41. //该测试用例实现在测试其他用例之前,测试确保获取的组件不为空
  42. public void testPreConditions()
  43. {
  44. assertNotNull(mLoginTest);
  45. assertNotNull(userName);
  46. assertNotNull(passWord);
  47. assertNotNull(login);
  48. assertNotNull(reset);
  49. }
  50. /**该方法实现在登录界面上输入相关的登录信息。由于UI组件的
  51. * 相关处理(如此处的请求聚焦)需要在UI线程上实现,
  52. * 所以需调用Activity的runOnUiThread方法实现。
  53. */
  54. public void input()
  55. {
  56. mLoginTest.runOnUiThread(new Runnable()
  57. {
  58. @Override
  59. public void run()
  60. {
  61. // TODO Auto-generated method stub
  62. userName.requestFocus();
  63. userName.performClick();
  64. }
  65. });
  66. /*由于测试用例在单独的线程上执行,所以此处需要同步application,
  67. * 调用waitForIdleSync等待测试线程和UI线程同步,才能进行输入操作。
  68. * waitForIdleSync和sendKeys不允许在UI线程里运行
  69. */
  70. mInstrumentation.waitForIdleSync();
  71. //调用sendKeys方法,输入用户名
  72. sendKeys(KeyEvent.KEYCODE_P, KeyEvent.KEYCODE_H,
  73. KeyEvent.KEYCODE_I, KeyEvent.KEYCODE_C,
  74. KeyEvent.KEYCODE_O, KeyEvent.KEYCODE_M,
  75. KeyEvent.KEYCODE_M);
  76. mLoginTest.runOnUiThread(new Runnable()
  77. {
  78. @Override
  79. public void run()
  80. {
  81. // TODO Auto-generated method stub
  82. passWord.requestFocus();
  83. passWord.performClick();
  84. }
  85. });
  86. //调用sendKeys方法,输入密码
  87. sendKeys(KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2,
  88. KeyEvent.KEYCODE_3, KeyEvent.KEYCODE_4);
  89. }
  90. //测试输入的用户信息
  91. public void testInput()
  92. {
  93. //调用测试类的input方法,实现输入用户信息(sendKeys实现输入)
  94. input();
  95. //测试验证用户信息的预期值是否等于实际值
  96. assertEquals("phicomm", userName.getText().toString());
  97. //密码的预期值123与实际值1234不符,Failure;
  98. assertEquals("123", passWord.getText().toString());
  99. }
  100. //测试登录按钮
  101. public void testLogin()
  102. {
  103. input();
  104. //开新线程,并通过该线程在实现在UI线程上执行操作
  105. mInstrumentation.runOnMainSync(new Runnable()
  106. {
  107. @Override
  108. public void run()
  109. {
  110. // TODO Auto-generated method stub
  111. login.requestFocus();
  112. login.performClick();
  113. }
  114. });
  115. }
  116. //测试重置按钮
  117. public void testReset()
  118. {
  119. input();
  120. mInstrumentation.runOnMainSync(new Runnable()
  121. {
  122. @Override
  123. public void run()
  124. {
  125. // TODO Auto-generated method stub
  126. reset.requestFocus();
  127. //点击按钮
  128. reset.performClick();
  129. }
  130. });
  131. //验证重置按钮的实现功能,是否点击后内容为空
  132. assertEquals("", userName.getText().toString());
  133. assertEquals("", passWord.getText().toString());
  134. }
  135. }

  运行该测试类进行测试(选中—->Run As—>Android Junit Test),然后会自动启动模拟器进行相关的输入点击测试。注:测试时可以发现,程序在测试到testLogin()方法登录到另一个界面时,测试就停止了,也就是说testReset()没测试到。所以,需要测试testReset()时可以先把testLogin()注释掉,不然程序会测试到testLogin()后就不在对testReset()进行测试。

 

FxResultActivity的测试类代码如下:

  1. package com.phicomm.hu.test;
  2. import android.content.Intent;
  3. import android.test.ActivityInstrumentationTestCase2;
  4. import android.widget.TextView;
  5. import com.phicomm.hu.FxResultActivity;
  6. public class FxResultActivityTest extends ActivityInstrumentationTestCase2<FxResultActivity>
  7. {
  8. private static final String LOGIN_INFO = "用户名:feixun\n密码:123";
  9. private FxResultActivity mResultActivity;
  10. private TextView result;
  11. public FxResultActivityTest()
  12. {
  13. super(FxResultActivity.class);
  14. }
  15. @Override
  16. protected void setUp() throws Exception
  17. {
  18. // TODO Auto-generated method stub
  19. super.setUp();
  20. //创建Intent,通过Intent传递用户的登录信息
  21. Intent intent = new Intent();
  22. intent.putExtra("userName", "feixun");
  23. intent.putExtra("passWord", "123");
  24. //通过携带用户登录信息的intent启动FxResultActivity
  25. mResultActivity = launchActivityWithIntent("com.phicomm.hu",
  26. FxResultActivity.class, intent);
  27. //获取UI组件
  28. result = (TextView)mResultActivity.findViewById(com.phicomm.hu.R.id.result);
  29. }
  30. //测试验证用户的登录信息
  31. public void testLoginInfo()
  32. {
  33. //验证预期值是否等于实际值
  34. assertEquals(LOGIN_INFO, result.getText().toString());
  35. }
  36. }

运行上面的测试类,结果正确。

 

本文转自:http://blog.csdn.net/stevenhu_223/article/details/8298858

版权声明:本文为sishuiliuyun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/sishuiliuyun/p/4431471.html