通过 Maven 测试获取 'SurefireExecutionException' 而 运行 TestNG 项目

Getting 'SurefireExecutionException' while running TestNG project via maven test

我有一个主 class,我想通过 Maven 运行,这个主 class 动态创建 testng.xml 文件,这就是测试开始的方式。但是当我通过 'Run as->maven test' 运行 我的项目时,我得到 'SurefireExecutionException' 。下面是 pom.xml:

<build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
          <configuration>
          <forkMode>never</forkMode>
          <parallel>tests</parallel>
          <threadCount>10</threadCount>
          </configuration>
        </plugin>
         <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>main.MainRunner</mainClass>
    <arguments>
    </arguments>
  </configuration>
</plugin>
      </plugins>
    </pluginManagement>
  </build>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>com.relevantcodes</groupId>
    <artifactId>extentreports</artifactId>
    <version>2.41.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.0.0</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.0</version>
</dependency>
   <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

  </dependencies>

这是堆栈跟踪:

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.683 s
[INFO] Finished at: 2018-12-20T19:07:20+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project Automata: There are test failures.
[ERROR] 
[ERROR] Please refer to D:\Git\automata\Automata\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] Exception in provider
[ERROR] org.apache.maven.surefire.booter.SurefireExecutionException: Exception in provider
[ERROR]     at org.apache.maven.plugin.surefire.InPluginVMSurefireStarter.runSuitesInProcess(InPluginVMSurefireStarter.java:91)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1166)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1011)
[ERROR]     at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:857)
[ERROR]     at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
[ERROR]     at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR]     at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR]     at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR]     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
[ERROR]     at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR]     at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR]     at org.apache.maven.cli.MavenCli.execute(MavenCli.java:956)
[ERROR]     at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:290)
[ERROR]     at org.apache.maven.cli.MavenCli.main(MavenCli.java:194)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[ERROR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[ERROR]     at java.lang.reflect.Method.invoke(Unknown Source)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
[ERROR]     at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[ERROR] Caused by: java.lang.NoClassDefFoundError: org/testng/ITestContext
[ERROR]     at java.lang.Class.getDeclaredMethods0(Native Method)
[ERROR]     at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
[ERROR]     at java.lang.Class.privateGetMethodRecursive(Unknown Source)
[ERROR]     at java.lang.Class.getMethod0(Unknown Source)
[ERROR]     at java.lang.Class.getMethod(Unknown Source)
[ERROR]     at org.apache.maven.surefire.util.ReflectionUtils.tryGetMethod(ReflectionUtils.java:60)
[ERROR]     at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isSuiteOnly(JUnit3TestChecker.java:66)
[ERROR]     at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isValidJUnit3Test(JUnit3TestChecker.java:61)
[ERROR]     at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.accept(JUnit3TestChecker.java:56)
[ERROR]     at org.apache.maven.surefire.junit.PojoAndJUnit3Checker.accept(PojoAndJUnit3Checker.java:44)
[ERROR]     at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:102)
[ERROR]     at org.apache.maven.surefire.junit.JUnit3Provider.scanClassPath(JUnit3Provider.java:147)
[ERROR]     at org.apache.maven.surefire.junit.JUnit3Provider.invoke(JUnit3Provider.java:91)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR]     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[ERROR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[ERROR]     at java.lang.reflect.Method.invoke(Unknown Source)
[ERROR]     at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:206)
[ERROR]     at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:161)
[ERROR]     at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:84)
[ERROR]     at org.apache.maven.plugin.surefire.InPluginVMSurefireStarter.runSuitesInProcess(InPluginVMSurefireStarter.java:87)
[ERROR]     ... 25 more
[ERROR] Caused by: java.lang.ClassNotFoundException: org.testng.ITestContext
[ERROR]     at java.net.URLClassLoader.findClass(Unknown Source)
[ERROR]     at java.lang.ClassLoader.loadClass(Unknown Source)
[ERROR]     at java.lang.ClassLoader.loadClass(Unknown Source)
[ERROR]     at org.apache.maven.surefire.booter.IsolatedClassLoader.loadClass(IsolatedClassLoader.java:100)
[ERROR]     ... 46 more
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

编辑:正如下面 Krishnan 所建议的,我在我的 pom.xml 文件中添加了 testng 依赖项,在这样做之后它看起来像 BeforeSuite 和 BeforeTest classes 正在工作,但实际测试是'工作,这就是我现在得到的:

Surefire suite started.....
In Before Test
Starting ChromeDriver 2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90) on port 25689
Only local connections are allowed.
[1545327410.041][WARNING]: Timed out connecting to Chrome, retrying...
Dec 20, 2018 11:06:53 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
new instance
Surefire suite ended.....
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 31.895 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.638 s
[INFO] Finished at: 2018-12-20T23:06:58+05:30
[INFO] ------------------------------------------------------------------------

如果能对此有所启发,我将不胜感激。

编辑:在此处针对上述查询提出了一个新问题:

错误 Caused by: java.lang.ClassNotFoundException: org.testng.ITestContext 表明您的类路径中缺少 testng 库。

请在您的 pom 文件中添加对 testng 的依赖,然后重试。你应该没事的。 (如下所示)

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.0.0-beta1</version>
</dependency>