如果没有TestNG尝试运行JUnit测试,我的Maven项目如何运行JUnit和TestNG?

时间:2016-12-07 09:29:52

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

我有一个Maven项目,我需要在其中执行JUnit和TestNG测试。我的num = 3 #take input from the user if num > 1: # if input is greater than 1 check for factors for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") break else: print(num,"is a prime number") #if input number is less than or equal to 1, it is not prime else: print(num,"is not a prime number") 设置了Maven Surefire插件,如下所示:

pom.xml

不幸的是,当我使用Surefire执行单元测试时,所有测试都正确执行,但TestNG似乎试图再次执行JUnit测试,并且日志中出现如下例外情况。

<!-- ... -->
<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>
<!-- ... -->

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

1 个答案:

答案 0 :(得分:3)

我认为我会分享我的答案,因为花了一点时间才弄明白,我的特定问题与遇到这种特殊堆栈跟踪的其他人的问题有些不同。

解决方案是将以下XML元素添加到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) 文件中的Surefire配置中。

pom.xml

Surefire documentation on running JUnit and TestNG tests together进一步记录了这一点。