用命令行运行单元测试

时间:2022-09-19 22:22:38
对于如下的manifest文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.calculator2.tests">

    <application>
        <uses-library android:name="android.test.runner" />
    </application>

    <instrumentation android:name="CalculatorLaunchPerformance"
        android:targetPackage="com.android.calculator2"
        android:label="Calculator Launch Performance">
    </instrumentation>

    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.android.calculator2"
        android:label="Calculator Functional Testset">
    </instrumentation>
</manifest>

使用命令行运行<instrumentation>时,格式如下:
adb shell am instrument <flag> 包名/完整组件名(包括路径)
包名指的是 package="com.android.calculator2.tests"

举例:
运行其<instrumentation android:name="CalculatorLaunchPerformance"时须将CalculatorLaunchPerformance的完整路径写上,就像:
adb shell am instrument -w com.android.calculator2.tests/ com.android.calculator2.tests.CalculatorLaunchPerformance

运行其<instrumentation android:name="android.test.InstrumentationTestRunner"时使用
adb shell am instrument -w com.android.calculator2.tests/android.test.InstrumentationTestRunner