android的单元测试主要采用instrumentation,instrumentation是执行application instrumentation的基类,它具有启动能力,用于监控其他类的工具类。本博文在一个简单android demo开发应用上,使用instrumentation类完成单元测试。 android demo MainActivity(具体代码见后) 待测函数: add():加法函数,不显示在ui上 sub():减法函数,不显示在ui上 changetextview():改变textview文字显示,通过监听button点击相应改变textview文本内容 multip():乘法函数,通过监听button点击相应,完成乘数和被乘数的获取,并将结果显示在ui控件上。 SampleTest(具体代码见后) 代码简单说明: 类继承: sampleTest继承InstrumentationTestCase,作为监控的具体实现的定制类 public class sampleTest extends InstrumentationTestCase { private MainActivity sample = null; 启动被测应用/被测类: sampleTest继承InstrumentationTestCase,可以使用getInstrumentation()函数获取Instrumentation对象,通过Instrumentation的startActivitySync()函数启动一个Activity,直到Activity启动后返,其中intent设置被启动的Activity Intent intent = new Intent(); intent.setClassName("com.example.jc.instrumentsample", MainActivity.class.getName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); sample = (MainActivity)getInstrumentation().startActivitySync(intent); 单元case的编写: 函数名:测试函数以test为key命名 函数逻辑:点击按钮,改变textview文本显示。具体细节:为了防止ui线程停顿/卡死,新建一个线程来完成测试行为,即PerFromClick(继承runnable),其中函数中的btn2.performClick函数为模拟ui上点击行为 验证:获取textview的文本,调用assertEquals来验证与期望是否一致 public void testActivity(){ Log.d("testActivity","testActivity"); SystemClock.sleep(1500); getInstrumentation().runOnMainSync(new PerFromClick(btn)); SystemClock.sleep(1500); assertEquals("hello andriod",textView.getText().toString()); } private class PerFromClick implements Runnable { Button btn2; public PerFromClick(Button button){ btn2 = button; } @Override public void run() { btn2.performClick(); } } AndroidManifest.xml配置: 额外配置: uses-library:使用的libraray配置 instrumentation:申明测试包名,单元测试的方式 <uses-library android:name="android.test.runner" /> <instrumentation android:targetPackage="com.example.jc.instrumentsample" android:name="android.test.InstrumentationTestRunner" /> 测试执行: build工程后,在终端输入命令: $ adb shell am instrument -w com.example.jc.instrumentsample/android.test.InstrumentationTestRunner com.example.jc.instrumentsample:包名 android.test.InstrumentationTestRunner:指明启动的单元测试类 运行结果: 执行命令后,会自动启动测试程序进行测试,测试结果会在终端显示。 单元测试一共执行了4个用例,其中3个失败(标红,失败的用力会有简单的log日志),1个通过(绿点,通过用例为点表示) com.example.jc.instrumentsample.sampleTest: Failure in testActivity junit.framework.ComparisonFailure: expected:<[hello andrio]d> but was:<[this is my first androi]d> at com.example.jc.instrumentsample.sampleTest.testActivity(sampleTest.java:84) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701) Failure in testAdd: junit.framework.AssertionFailedError: expected:<3> but was:<2> at com.example.jc.instrumentsample.sampleTest.testAdd(sampleTest.java:102) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701) . Failure in testSub: junit.framework.AssertionFailedError: expected:<1> but was:<-1> at com.example.jc.instrumentsample.sampleTest.testSub(sampleTest.java:109) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701) Test results for InstrumentationTestRunner=.F.F.. Time: 6.338 FAILURES!!! Tests run: 4, Failures: 3, Errors: 0