maven运行Robotium测试case

Robotium是Android应用的自动化测试工具,类似于Selenium,我们可以通过编写java代码来测试android程序。 maven是个项目管理工具,可以很方面地对项目进行管理、构建、发布等。利用maven构建,可以很方便地放到hudson、jenkins等集成环境中。

  1. 安装maven,设置环境变量,不在多说。
  2. 安装android sdk,设置环境变量。 下载sdk后,在Android SDK Manager中安装Android SDK Tools、Android SDK Platform-tools(注意最好安装16.02,不要安装17,17版本很多工具找不到,造成maven-android-plugin无法使用)、android SDK build-tools和android 4.2.2(API 17)。
  3. 安装好android sdk后,在AVD Manager中新建一个android虚拟机,应该可以正常启动了。
  4. 下载Maven Android SDK Deployer插件,让maven支持android,项目在github上,地址https://github.com/mosabua/maven-android-sdk-deployer,下载后,解压到本地目录。进入其根目录执行mvn install -P 4.2(这个版本要和第2步中下载的api对应,api 17对应android 4.2)。

    [INFO] Maven Android SDK Deployer ........................ SUCCESS [0.190s]
    [INFO] Android Platforms ................................. SUCCESS [0.000s]
    [INFO] Android Platform 4.2 API 17 ....................... SUCCESS [1.983s]
    [INFO] Android Add-Ons ................................... SUCCESS [0.030s]
    [INFO] Android Add-On Google Platform 4.2 API 17 (Maps and USB) SUCCESS [0]
    [INFO] Android Extras .................................... SUCCESS [0.010s]
    如果报错,应该是android sdk中的包没有下全,不过,我们只需要Android Platform 4.2 API 17这个成功即可。

  5. 首先利用maven命令行创建一个android-quickstart-archetype,命令如下:

    mvn archetype:generate -DarchetypeArtifactId=android-quickstart -DarchetypeGroupId=de.akquinet.android.archetypes -DarchetypeVersion=1.0.8 -DgroupId=com.yeetrack -DartifactId=androidTest

  6. 将此项目import到eclipse或者idea中,修改项目的pom.xml文件
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yeetrack</groupId>
    <artifactId>mavenAndroid</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>mavenAndroid</name>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <platform.version>4.0.1.2</platform.version>
        <sdk.dir>D:\work\android-sdk-windows</sdk.dir>
    </properties>
    
    <dependencies>
        <!-- <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>${platform.version}</version>
        <scope>provided</scope>
        </dependency>-->
        <dependency>
            <groupId>com.jayway.android.robotium</groupId>
            <artifactId>robotium-solo</artifactId>
            <version>4.1</version>
        </dependency>
        <!--在maven仓库中没有找到android.jar,所以使用本地的jar包-->
        <dependency>
            <groupId>com.google</groupId>
            <artifactId>android</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/android.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>emma</groupId>
            <artifactId>emma</artifactId>
            <version>2.1.5320</version>
        </dependency>      
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                    <resourceDirectory>${project.basedir}/res</resourceDirectory>
                    <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                    <sdk>
                        <path>${sdk.dir}</path>
                        <platform>17</platform>
                    </sdk>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  7. 修改项目的AndroidManifest.xml文件,我的项目AndroidManifest.xml文件如下:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yeetrack.mavenAndroid" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
    
    <!--指定要测试的软件包-->
    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.yeetrack.androidapp" />
    
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="android.test.runner" />
    </application>
    
  8. 写完测试代码后,直接mvn clean install即可。既然maven跑通了,利用jenkins、hudson自动化构建robotium case都可以很简单地实现,不再叙述。
    [INFO] android.device parameter not set, using all attached devices
    [INFO] Successfully installed C:\oa-robotium\target\mavenAndroid-1.0-SNAPSHOT.apk to emulator-5554_android-4.2.2_unknown_sdk
    [INFO]
    [INFO] --- android-maven-plugin:3.1.1:internal-integration-test (default-internal-integration-test) @ mavenAndroid ---
    [INFO] Found 1 devices connected with the Android Debug Bridge
    [INFO] android.device parameter not set, using all attached devices
    [INFO] Running instrumentation tests in com.yeetrack.mavenAndroid on emulator-5554
    (avdName=android-4.2.2)
    [INFO]   Run started: com.yeetrack.mavenAndroid, 2 tests:
    [INFO]     Start: com.yeetrack.oa.robotium.test.BusInfoTest#testBus
    [INFO]     End: com.yeetrack.oa.robotium.test.BusInfoTest#testBus
    [INFO]     Start: com.yeetrack.oa.robotium.test.LoginTest#testLogin
    [INFO]     End: com.yeetrack.oa.robotium.test.LoginTest#testLogin
    [INFO]   Run ended: 36197 ms
    [INFO]   Tests run: 2,  Failures: 0,  Errors: 0
    [INFO] Report file written to C:\oa-robotium\target\surefire-reports\TEST-emulator-5554_android-4.2.2_unknown_sdk.xml
    [INFO]
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ mavenAndroid ---
    
    [INFO] Installing C:\oa-robotium\target\mavenAndroid-1.0-SNAPSHOT.apk to C:\Users\youthflies\.m2\repository\com\yeetrack\mavenAndroid\1.0-SNAPSHOT\mavenAndroid-1.0-SNAPSHOT.apk
    [INFO] Installing C:\oa-robotium\pom.xml to C:\Users\youthflies\.m2\repository\com\yeetrack\mavenAndroid\1.0-SNAPSHOT\mavenAndroid-1.0-SNAPSHOT.pom
    [INFO] Installing C:\oa-robotium\target\mavenAndroid-1.0-SNAPSHOT.jar to C:\Users\youthflies\.m2\repository\com\yeetrack\mavenAndroid\1.0-SNAPSHOT\mavenAndroid-1.0-SNAPSHOT.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1:45.668s
    [INFO] Finished at: Sat May 25 15:01:19 CST 2013
    [INFO] Final Memory: 17M/223M
    [INFO] ------------------------------------------------------------------------
版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=578