【问题标题】:Jenkins and JUnit annotationsJenkins 和 JUnit 注释
【发布时间】:2011-12-14 16:51:35
【问题描述】:

我的测试套件和 Jenkins 存在一些问题。

我的 TestSuite 看起来就像这样:

@RunWith(Suite.class)
@SuiteClasses( { CompanyRepositoryTest.class,
StudentRepositoryTest.class })
public class ServiceTestSuite {}

在本地主机上运行这个测试套件就像一个魅力,总共 7 次测试显示成功。然而,当测试套件由 Jenkins 运行时,它说找不到测试:

junit.framework.AssertionFailedError: No tests found in com.example.suite.ServiceTestSuite

我假设这与 Jenkins 未获取的注释有关。我可以做些什么来纠正这个问题吗?

编辑: 这是我们 Ant build.xml 中的测试部分。

    <junit fork="yes" printsummary="withOutAndErr" >
        <formatter type="xml"/>
        <test name="test.ibm.teknikspranget.suite.ServiceTestSuite" todir="${junit.output.dir}"/>
        <classpath refid="compile.classpath"/>
    </junit>

所以我们实际上是在尝试从 Ant 运行测试套件。 我们是否应该运行每个单独的测试?看起来我们正在使用 Jenkins 运行 Ant 1.8.2,所以这应该不是问题。

【问题讨论】:

    标签: unit-testing junit jenkins


    【解决方案1】:

    这是因为您将其作为 JUnit 3 测试运行,而不是 JUnit 4 测试。您可以这样说,因为 junit.framework.* 类是 JUnit 3,但 org.junit.* 类是 JUnit 4。您的错误消息是:

    junit.framework.AssertionFailedError: No tests found in com.example.suite.ServiceTestSuite
    

    如果您使用 JUnit 3 运行程序运行测试,那么它将在您的 TestSuite 中查找名为 suite() 的方法,它不会使用注释。您需要使用 JUnit 4 测试运行程序运行它,例如 org.junit.runner.JUnitCore 或类似的。

    如何解决这个问题取决于您在 Jenkins 中如何调用它。如果您正在运行 ant,请使用高于 1.7 的版本,它应该可以工作。

    如果您使用的是 maven,则使用 JUnit 库 > 4 的版本应该可以工作,请尝试 4.11。如果由于某种原因这仍然不起作用,您可以通过将以下内容添加到您的 pom 来强制提供程序为junit 4:

    <plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.11</version>
        <dependencies>
          <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.11</version>
          </dependency>
        </dependencies>
      </plugin>
    [...]
    </plugins>
    

    来自:Surefire: Using JUnit

    【讨论】:

    • 非常感谢!明天我会调查这第一件事。我们使用的是 Ant,所以我想看版本是第一步。
    • 它工作正常! Jenkins 被配置为使用“默认”Ant 版本,无论这意味着什么。通过强制它使用 1.8.2,我们再次运行测试!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2012-07-29
    • 2015-04-08
    • 1970-01-01
    • 2017-12-07
    • 2016-03-08
    相关资源
    最近更新 更多