传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
今天我们学习如何在Android应用中进行单元测试,分别测试业务方法和日志系统,并且给出相关的log记录,下面给出该场景的案例。
1案例技术要点
(1)为了使你的应用程序支持单元测试功能,需要在清单文件(AndroidManifest.xml)中添加如下配置:
<uses-library android:name="android.test.runner" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.lynn.testing" android:label="Tests for My App" />
(2)相关测试类需要继承android.test.AndroidTestCase类方可进行单元测试。
2案例代码陈列
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.lynn.testing" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="cn.lynn.testing.UnitTestingMainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="android.test.runner" /> </application> <uses-sdk android:minSdkVersion="8" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.lynn.testing" android:label="Tests for My App" /> </manifest>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Android应用单元测试</string> </resources>
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="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
业务服务类:PersonService.java
package cn.lynn.testing; public class PersonService { public void save(String username) { System.out.println(username); } public int add(int a, int b) { return a + b; } }
业务测试类:PersonServiceTest.java
package cn.lynn.testing; import junit.framework.Assert; import android.test.AndroidTestCase; public class PersonServiceTest extends AndroidTestCase { public void testSave() throws Exception { PersonService service = new PersonService(); service.save("lynnli1229"); } public void testAdd() throws Exception { PersonService service = new PersonService(); int actual = service.add(1, 2); Assert.assertEquals(8, actual);// 1+2等于8为true,否则为false } }
日志测试类:LogTest.java
package cn.lynn.testing; import android.test.AndroidTestCase; import android.util.Log; public class LogTest extends AndroidTestCase { private static final String TAG = "LogTest"; public void testOutLog1() throws Throwable { Log.i(TAG, "www.csdn.cn"); } public void testOutLog2() throws Throwable { System.out.println("www.csdn.cn"); } public void testOutLog3() throws Throwable { System.err.println("www.sohu.cn"); } }
UnitTestingMainActivity.java
package cn.lynn.testing; import android.app.Activity; import android.os.Bundle; public class UnitTestingMainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
3案例效果展示
4案例日志输出
06-02 14:45:23.410: I/TestRunner(683): started: testAdd(cn.lynn.testing.PersonServiceTest) 06-02 14:45:23.460: I/TestRunner(683): finished: testAdd(cn.lynn.testing.PersonServiceTest) 06-02 14:45:23.460: I/TestRunner(683): passed: testAdd(cn.lynn.testing.PersonServiceTest) 06-02 14:45:23.511: I/TestRunner(683): started: testSave(cn.lynn.testing.PersonServiceTest) 06-02 14:45:23.529: I/TestRunner(683): finished: testSave(cn.lynn.testing.PersonServiceTest) 06-02 14:45:23.529: I/TestRunner(683): passed: testSave(cn.lynn.testing.PersonServiceTest) 06-02 14:45:23.599: I/TestRunner(683): started: testAndroidTestCaseSetupProperly(cn.lynn.testing.PersonServiceTest) 06-02 14:45:23.610: I/TestRunner(683): finished: testAndroidTestCaseSetupProperly(cn.lynn.testing.PersonServiceTest) 06-02 14:45:23.610: I/TestRunner(683): passed: testAndroidTestCaseSetupProperly(cn.lynn.testing.PersonServiceTest) 06-02 14:45:28.329: I/TestRunner(698): started: testAdd(cn.lynn.testing.PersonServiceTest) 06-02 14:45:28.369: I/TestRunner(698): failed: testAdd(cn.lynn.testing.PersonServiceTest) 06-02 14:45:28.390: I/TestRunner(698): ----- begin exception ----- 06-02 14:45:29.199: I/TestRunner(698): junit.framework.AssertionFailedError: expected:<8> but was:<3> 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.Assert.fail(Assert.java:47) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.Assert.failNotEquals(Assert.java:282) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.Assert.assertEquals(Assert.java:64) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.Assert.assertEquals(Assert.java:201) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.Assert.assertEquals(Assert.java:207) 06-02 14:45:29.199: I/TestRunner(698): at cn.lynn.testing.PersonServiceTest.testAdd(PersonServiceTest.java:16) 06-02 14:45:29.199: I/TestRunner(698): at java.lang.reflect.Method.invokeNative(Native Method) 06-02 14:45:29.199: I/TestRunner(698): at java.lang.reflect.Method.invoke(Method.java:521) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.TestCase.runTest(TestCase.java:154) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.TestCase.runBare(TestCase.java:127) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.TestResult$1.protect(TestResult.java:106) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.TestResult.runProtected(TestResult.java:124) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.TestResult.run(TestResult.java:109) 06-02 14:45:29.199: I/TestRunner(698): at junit.framework.TestCase.run(TestCase.java:118) 06-02 14:45:29.199: I/TestRunner(698): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) 06-02 14:45:29.199: I/TestRunner(698): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) 06-02 14:45:29.199: I/TestRunner(698): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520) 06-02 14:45:29.199: I/TestRunner(698): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447) 06-02 14:45:29.199: I/TestRunner(698): ----- end exception ----- 06-02 14:45:29.319: I/TestRunner(698): finished: testAdd(cn.lynn.testing.PersonServiceTest) 06-02 14:45:29.361: I/TestRunner(698): started: testSave(cn.lynn.testing.PersonServiceTest) 06-02 14:45:29.369: I/System.out(698): lynnli1229 06-02 14:45:29.369: I/TestRunner(698): finished: testSave(cn.lynn.testing.PersonServiceTest) 06-02 14:45:29.369: I/TestRunner(698): passed: testSave(cn.lynn.testing.PersonServiceTest) 06-02 14:45:29.380: I/TestRunner(698): started: testAndroidTestCaseSetupProperly(cn.lynn.testing.PersonServiceTest) 06-02 14:45:29.390: I/TestRunner(698): finished: testAndroidTestCaseSetupProperly(cn.lynn.testing.PersonServiceTest) 06-02 14:45:29.390: I/TestRunner(698): passed: testAndroidTestCaseSetupProperly(cn.lynn.testing.PersonServiceTest)
06-02 14:40:09.460: I/TestRunner(635): started: testOutLog1(cn.lynn.testing.LogTest) 06-02 14:40:09.589: I/TestRunner(635): finished: testOutLog1(cn.lynn.testing.LogTest) 06-02 14:40:09.589: I/TestRunner(635): passed: testOutLog1(cn.lynn.testing.LogTest) 06-02 14:40:09.644: I/TestRunner(635): started: testOutLog2(cn.lynn.testing.LogTest) 06-02 14:40:09.650: I/TestRunner(635): finished: testOutLog2(cn.lynn.testing.LogTest) 06-02 14:40:09.659: I/TestRunner(635): passed: testOutLog2(cn.lynn.testing.LogTest) 06-02 14:40:09.699: I/TestRunner(635): started: testOutLog3(cn.lynn.testing.LogTest) 06-02 14:40:09.839: I/TestRunner(635): finished: testOutLog3(cn.lynn.testing.LogTest) 06-02 14:40:09.839: I/TestRunner(635): passed: testOutLog3(cn.lynn.testing.LogTest) 06-02 14:40:09.893: I/TestRunner(635): started: testAndroidTestCaseSetupProperly(cn.lynn.testing.LogTest) 06-02 14:40:09.900: I/TestRunner(635): finished: testAndroidTestCaseSetupProperly(cn.lynn.testing.LogTest) 06-02 14:40:09.900: I/TestRunner(635): passed: testAndroidTestCaseSetupProperly(cn.lynn.testing.LogTest) 06-02 14:40:14.709: I/TestRunner(650): started: testOutLog1(cn.lynn.testing.LogTest) 06-02 14:40:14.841: I/LogTest(650): www.csdn.cn 06-02 14:40:14.841: I/TestRunner(650): finished: testOutLog1(cn.lynn.testing.LogTest) 06-02 14:40:14.893: I/TestRunner(650): passed: testOutLog1(cn.lynn.testing.LogTest) 06-02 14:40:14.900: I/TestRunner(650): started: testOutLog2(cn.lynn.testing.LogTest)