【问题标题】:How to run integration tests for a mavenised Android application?如何为 mavenized Android 应用程序运行集成测试?
【发布时间】:2013-08-11 14:03:29
【问题描述】:

我想运行一些测试,检查活动 A 在启动活动 B 时是否将一些信息放入 Intent。

这些测试要求有 Android 基础架构。

如何实现它们以便可以使用 Maven 运行它们?

我试过了

  1. 为此使用 akquinet 原型并使用单独的 Mave 项目进行集成测试,但 it never worked(我的 Robolectric 测试未执行)和
  2. 在与主应用程序相同的 Maven 项目中运行它们,这导致 ClassNotFoundErrors 由 Robolectric 引起(当我将 Roboelectric 添加到依赖项时,我无法 mvn install 我的项目,因为我收到与类路径相关的错误对于非 Robolectric 课程)。

更新 1 (23.08.2013): 父项目的 POM:

<?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.mycompany</groupId>
    <artifactId>my-product-parent</artifactId>
    <version>1.3.0</version>
    <packaging>pom</packaging>
    <name>my-product - Parent</name>

    <modules>
        <module>my-product</module>
        <module>my-product-it</module>
    </modules>

    <properties>
        <platform.version> 4.1.1.4
                    </platform.version>
        <android.plugin.version>3.6.0</android.plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.android</groupId>
                <artifactId>android</artifactId>
                <version>${platform.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.google.android</groupId>
                <artifactId>android-test</artifactId>
                <version>${platform.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>${android.plugin.version}</version>
                    <configuration>
                        <sdk>
                            <platform>16</platform>
                        </sdk> 
                                            </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

实际应用项目的POM:

<?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>

    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>my-product-parent</artifactId>
        <version>1.3.0</version>
    </parent>

    <groupId>com.mycompany</groupId>
    <artifactId>my-product</artifactId>
    <version>1.4.0</version>
    <packaging>apk</packaging>
    <name>my-product - Application</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <powermock.version>1.5</powermock.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>some-library</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>commons</artifactId>
            <version>2.5</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.inject</groupId>
                    <artifactId>guice</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!-- Testing (start) -->
        <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-assert-core</artifactId>
            <version>2.0M8</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>

        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <!-- Testing (end) -->
        <!-- Google Maps (start) -->
        <!-- 
        <dependency>
            <groupId>com.google.android.maps</groupId>
            <artifactId>maps</artifactId>
            <version>16_r3</version>
            <scope>provided</scope>
        </dependency>


 -->
        <dependency>
            <groupId>com.google.android.gms</groupId>
            <artifactId>google-play-services</artifactId>
            <version>6</version>
            <type>apklib</type>
        </dependency>

        <dependency>
            <groupId>com.google.android.gms</groupId>
            <artifactId>google-play-services</artifactId>
            <version>6</version>
            <type>jar</type>
        </dependency>
        <!-- Google Maps (end) -->
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

集成测试项目的POM:

<?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>

    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>my-product-parent</artifactId>
        <version>1.3.0</version>
    </parent>

    <artifactId>my-product-it</artifactId>
    <packaging>apk</packaging>
    <name>my-product-it - Integration tests</name>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android-test</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>my-product</artifactId>
            <type>apk</type>
            <version>1.3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>my-product</artifactId>
            <type>jar</type>
            <version>1.3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.9.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>                
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.easytesting</groupId>
            <artifactId>fest-assert-core</artifactId>
            <version>2.0M8</version>
        </dependency>

        <!-- Make sure this (robolectric dependency) is below the android dependencies -->
        <dependency>
            <groupId>com.pivotallabs</groupId>
            <artifactId>robolectric</artifactId>
            <version>1.0-RC4</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.inject</groupId>
                    <artifactId>guice</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.objenesis</groupId>
                    <artifactId>objenesis</artifactId>
                </exclusion>
                <!-- 
                <exclusion>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                </exclusion>
                 -->
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <configuration>
                    <test>
                        <!--<skip>true|false|auto</skip> -->
                        <!--<instrumentationPackage>packageName</instrumentationPackage> -->
                        `
                        <!--<instrumentationRunner>className</instrumentationRunner> -->
                        <!--<debug>true|false</debug> -->
                        <!--<coverage>true|false</coverage> -->
                        <!--<logonly>true|false</logonly> avd -->
                        <!--<testsize>small|medium|large</testsize> -->
                        <createReport>true</createReport>

                        <classes>
                            <class>com.mycompany.cb.android.test.AcceptInvitationActivityTest</class>
                        </classes>

                        <!--<classes> -->
                        <!--<class>your.package.name.YourTestClass</class> -->
                        <!--</classes> -->
                        <!--<packages> -->
                        <!--<package>your.package.name</package> -->
                        <!--</packages> -->
                    </test>
                </configuration>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 请发布您的 POM。
  • @TheTerribleSwiftTomato 查看我的更新 1。

标签: java android maven testing robolectric


【解决方案1】:

请查看 Github Android 应用源代码。 https://github.com/github/android

这个项目有一个非常好的 maven 设置,带有集成测试。他们使用Roboguicemaven-android-plugin

希望对你有帮助。

【讨论】:

  • 这看起来很有帮助。克隆下来试了一下,肯定会偷到那里的一些指针。
【解决方案2】:

本项目集成了很多测试工具以及代码质量工具:https://github.com/stephanenicolas/Quality-Tools-for-Android

我编写了一个 bash 脚本,它生成了一个配置了 robolectric、spoon 和 robotsium 的 mavenized android 项目:https://github.com/marsucsb/mvn-android-project

【讨论】:

    猜你喜欢
    • 2013-10-05
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2015-12-25
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多