【问题标题】:Maven fails to run unit tests on cloudbeesMaven 无法在 cloudbees 上运行单元测试
【发布时间】:2013-07-30 12:06:41
【问题描述】:

我有一个基于 Maven 的 Android 项目设置,用于 cloudbees 上的 ci。我的 Jenkins 工作只是在从 BitBucket 检索 src 后运行“安装”目标。一切都很好。唯一的问题是单元测试无法运行。

在 IntelliJ(本地)中运行完全相同的目标确实会找到并运行单元测试。

我的 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.odl</groupId>
    <artifactId>GhostRunner</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>GhostRunner</name>

    <properties>
        <platform.version> 4.1.1.4
            </platform.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${platform.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.robolectric</groupId>
            <artifactId>robolectric</artifactId>
            <version>2.0-alpha-1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-android</artifactId>
            <version>4.45</version>
        </dependency>
        <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-core</artifactId>
            <version>4.45</version>
        </dependency>
        <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-jdbc</artifactId>
            <version>4.45</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.8.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>${project.basedir}/assets</assetsDirectory>
                    <resourceDirectory>${project.basedir}/res</resourceDirectory>
                    <nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
                    <sdk>
                        <platform>16</platform>
                    </sdk>
                                        <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.15</version>
            </plugin>
        </plugins>
    </build>
</project>

jenkins 输出是这样的 -->

[INFO] --- maven-surefire-plugin:2.15:test (default-test) @ GhostRunner ---
[INFO] No tests to run.
mojoSucceeded org.apache.maven.plugins:maven-surefire-plugin:2.15(default-test)
[JENKINS] Recording test results
mojoStarted com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.0(default-emma) 

另一个有趣的点是,我可以删除 surefire 依赖项,并且单元测试仍然可以针对 IntelliJ 中的安装目标正常运行。由于某种原因,Maven 在 Cloudbees 上从 Jenkins 运行时看不到测试。

感激地接受智慧。

这是 cloudbees 不得不说的 ::

这里的问题是 IntelliJ 正在为您编译测试类 并将这些类放入 ${project.build.testOutputDirectory} 给你。

您可以通过让 IntelliJ 执行来在本地复制此故障 全新安装(按住 ⌘ 同时单击生命周期目标)

有两种方法可以解决这个问题。

告诉 Maven 你的测试源不在标准位置

将您的测试源放入标准位置。

当您处理大量插件时,其中一些未编写 由 Apache Maven 团队或 Codehaus Mojo 团队,与 标准目录布局是最好的方法。插件受 对 Apache Maven 或 Codehaus Mojo 社区的审查通常会 当你告诉 Maven 你正在使用一个 非标准布局。不受该审查过程约束的插件可以 通常不知道如何正确工作,因此您最终会 强制与标准布局对齐。

斯蒂芬·康诺利 CloudBees, Inc

我已重新组织我的项目以反映 Maven 规范,但它仍未在 cloudbees 服务器上运行测试。

这是来自 jenkins 控制台输出的相关输出::

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ GhostRunner ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /scratch/jenkins/workspace/ghost runner/src/test/resources
mojoSucceeded org.apache.maven.plugins:maven-resources-plugin:2.6(default-testResources)
mojoStarted org.apache.maven.plugins:maven-compiler-plugin:3.0(default-testCompile)
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ GhostRunner ---
mojoSucceeded org.apache.maven.plugins:maven-compiler-plugin:3.0(default-testCompile)
mojoStarted org.apache.maven.plugins:maven-surefire-plugin:2.15(default-test)
[INFO] 
[INFO] --- maven-surefire-plugin:2.15:test (default-test) @ GhostRunner ---
[INFO] No tests to run.

我想重要的是

没有可编译的源代码

不知何故,它仍然不知道测试在哪里。

【问题讨论】:

  • 你的单元测试在哪里? src/test/java 并且您将它们命名为 *Test*.java, *Test.java and *TestCase.java
  • 它们在 /src/test 下并且都以 Test 的格式命名。你觉得test文件夹下有java文件夹重要吗?
  • 是的,这是必要的,因为它是 default in Maven src/main/java 用于生产代码 src/test/java 用于单元测试。此外,src/main/resources 用于生产代码中的资源,src/test/resources 用于单元测试中的资源。
  • cloudbees 的人支持你说的话::

标签: android unit-testing maven cloudbees


【解决方案1】:

根据上述问题中的 cmets,您似乎没有将测试源文件放入 Maven 预期的目录结构中。这实际上是 Maven 配置问题,与 CloudBees 没有直接关系(除了 CloudBees 使用 vanilla maven 来运行您的构建)。如果您尝试通过 maven 'mvn' 命令在本地运行自己的构建(就像 CloudBees Jenkins CI 作业一样),您应该会遇到同样的错误。

根据standard Maven directory structure,您应该将测试的源代码放在:src/test/java

如果您想要/需要更改默认的 maven 目录结构,通常可以在 pom.xml 中提供配置更改来设置这些。但是,很多人发现与maven对抗只会带来更多的痛苦,最终会回到标准。

有关更多信息,请查看有关 how to change the default Maven tests directory 的这个 stackoverflow 线程。

【讨论】:

  • 这很有趣。我已经修改了我的项目结构,将所有 src 代码放在预期的位置(如上所述),并且测试仍然无法在 cloudbees 上运行。如果我在本地使用 'mvn clean' 和 'mvn install' 从 cmd 行运行,则测试全部运行。问题似乎是由于某种原因 cloudbees 构建找不到测试源。
  • 请在 CloudBees 上开一张支持票,我们可以帮助您深入了解。
  • 感谢 Cloudbees 的所有人帮助我。最后的问题是我。在重新安排我的项目结构时,我以某种方式设法从版本控制中删除了大部分(源代码),因此它不会在 IntelliJ 中显示为“更改”。本地一切正常,但 bitbucket 存储库是空的。必须更加努力:)
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
相关资源
最近更新 更多