【问题标题】:How can my Maven project run both JUnit and TestNG without TestNG trying to run JUnit tests?我的 Maven 项目如何在没有 TestNG 尝试运行 JUnit 测试的情况下同时运行 JUnit 和 TestNG?
【发布时间】:2016-12-07 09:29:51
【问题描述】:

我有一个 Maven 项目,我需要在其中执行 JUnit 和 TestNG 测试。我的pom.xml 已将 Maven Surefire 插件设置如下:

<!-- ... -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <argLine>-Xmx1024m</argLine>
    <threadCount>1</threadCount>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-testng</artifactId>
      <version>2.19.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-junit47</artifactId>
      <version>2.19.1</version>
    </dependency>
  </dependencies>
</plugin>
<!-- ... -->

不幸的是,当我使用 Surefire 执行单元测试时,所有测试都正确执行,但 TestNG 似乎正在尝试第二次执行 JUnit 测试,并且日志中出现如下异常。

org.testng.TestNGException: 
Failure in JUnit mode for class com.hotwire.api.pricing.impl.PricingConfigIT: could not create/run JUnit test suite: 
Cannot find JUnit method class junit.framework.TestSuite$1.warning
at org.testng.junit.JUnitTestRunner.runFailed(JUnitTestRunner.java:239)
at org.testng.junit.JUnitTestRunner.start(JUnitTestRunner.java:232)
at org.testng.junit.JUnitTestRunner.run(JUnitTestRunner.java:213)
at org.testng.TestRunner$1.run(TestRunner.java:672)
at org.testng.TestRunner.runWorkers(TestRunner.java:1003)
at org.testng.TestRunner.privateRunJUnit(TestRunner.java:703)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1170)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1095)
at org.testng.TestNG.run(TestNG.java:1007)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:132)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:198)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:94)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:147)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)

如何防止此异常污染我的日志?

【问题讨论】:

    标签: java maven junit testng maven-surefire-plugin


    【解决方案1】:

    我想我会分享我的答案,因为我花了一点时间才弄清楚,而且我的特定问题与遇到此特定堆栈跟踪的其他人的问题有些不同。

    解决方案是在pom.xml文件中的Surefire配置中添加以下XML元素。

    <configuration>
      <!-- pre-existing config -->
      <properties>
        <property>
          <name>junit</name>
          <value>false</value>
        </property>
      </properties>
    </configuration>
    

    这在Surefire documentation on running JUnit and TestNG tests together 中有进一步的记录。

    【讨论】:

      猜你喜欢
      • 2014-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2023-04-04
      • 2020-01-07
      • 2013-07-22
      • 2010-11-16
      相关资源
      最近更新 更多