Jenkins and JUnit annotations



我的测试套件和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,所以这应该不是问题。

这是因为您将其作为JUnit 3测试运行,而不是JUnit 4测试。可以看出这一点,因为JUnit .framework.*类是JUnit 3,而org.junit. *类是JUnit 3。*类是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

最新更新