【IT168 技术文档】在网络上找了半天,也没有找正确的Android的测试办法,最后还是自己琢磨出来的。以前就听我朋友说过,android毛病一堆,现在才体会到。 Android SDK 和 iphone SDK 比,就不是一个档次的东西。网上总是有人说,Android还年轻,要体谅。我可不同意,要知道,android都已经出了好几个版本了,而现在最新的 1.5版和iphone sdk的beta版都比不上。
先说说它的单元测试吧,基于1.5和eclipse。这里我说的是我的思路,有更加好的思路,欢迎指导。
我的方法:如果你要测试的不是Activity或者Service,就用AndroidTestCase;否则选择:ActivityUnitTestCase,ServiceTestCase。
在做单元测试以前,你需要创建一个新的项目,把要测试的项目包含到java build path中来。项目中的AndroidManifest.xml如下(这里假设你的your.test.package里面包含你的 tests,your.work.package包含被测试的类):
Xml代码
1. <?xml version="1.0" encoding="utf-8"?> 2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3. package="your.tests.package"> 4. <application> 5. <uses-library android:name="android.test.runner" /> 6. </application> 7. <instrumentation android:name="android.test.InstrumentationTestRunner" 8. android:targetPackage="your.work.package" 9. android:label="Tests for Api Demos."/> 10. 11. </manifest> |
那么就可以做测试了,给出下面的实例;
1.用AndroidTestCase;
Java代码
1. public class SdcardTest extends AndroidTestCase { 2. public void test1(){ 3. // Log.v() 4. File f=new File("/sdcard"); 5. String[] l=f.list(); 6. this.assertTrue(f.exists()); 7. this.assertTrue(f.isDirectory()); 8. this.assertTrue(f.list().length>0); 9. } 10. } |
2.用ActivityUnitTestCase
Java代码
1. public class ForwardingTest extends ActivityUnitTestCase<Forwarding> { 2. 3. private Intent mStartIntent; 4. private Button mButton; 5. 6. public ForwardingTest() { 7. super(Forwarding.class); 8. } 9. 10. @Override 11. protected void setUp() throws Exception { 12. super.setUp(); 13. 14. // In setUp, you can create any shared test data, or set up mock components to inject 15. // into your Activity. But do not call startActivity() until the actual test methods. 16. mStartIntent = new Intent(Intent.ACTION_MAIN); 17. } 18. 19. /** 20. * The name 'test preconditions' is a convention to signal that if this 21. * test doesn't pass, the test case was not set up properly and it might 22. * explain any and all failures in other tests. This is not guaranteed 23. * to run before other tests, as junit uses reflection to find the tests. 24. */ 25. @MediumTest 26. public void testPreconditions() { 27. startActivity(mStartIntent, null, null); 28. mButton = (Button) getActivity().findViewById(R.id.go); 29. 30. assertNotNull(getActivity()); 31. assertNotNull(mButton); 32. } 33. 34. /** 35. * This test demonstrates examining the way that activity calls startActivity() to launch 36. * other activities. 37. */ 38. @MediumTest 39. public void testSubLaunch() { 40. Forwarding activity = startActivity(mStartIntent, null, null); 41. mButton = (Button) activity.findViewById(R.id.go); 42. 43. // This test confirms that when you click the button, the activity attempts to open 44. // another activity (by calling startActivity) and close itself (by calling finish()). 45. mButton.performClick(); 46. 47. assertNotNull(getStartedActivityIntent()); 48. assertTrue(isFinishCalled()); 49. } 50. 51. /** 52. * This test demonstrates ways to exercise the Activity's life cycle. 53. */ 54. @MediumTest 55. public void testLifeCycleCreate() { 56. Forwarding activity = startActivity(mStartIntent, null, null); 57. 58. // At this point, onCreate() has been called, but nothing else 59. // Complete the startup of the activity 60. getInstrumentation().callActivityOnStart(activity); 61. getInstrumentation().callActivityOnResume(activity); 62. 63. // At this point you could test for various configuration aspects, or you could 64. // use a Mock Context to confirm that your activity has made certain calls to the system 65. // and set itself up properly. 66. 67. getInstrumentation().callActivityOnPause(activity); 68. 69. // At this point you could confirm that the activity has paused properly, as if it is 70. // no longer the topmost activity on screen. 71. 72. getInstrumentation().callActivityOnStop(activity); 73. 74. // At this point, you could confirm that the activity has shut itself down appropriately, 75. // or you could use a Mock Context to confirm that your activity has released any system 76. // resources it should no longer be holding. 77. 78. // ActivityUnitTestCase.tearDown(), which is always automatically called, will take care 79. // of calling onDestroy(). 80. } 81. 82. } |