【问题标题】:Unable to run testNG tests from maven无法从 Maven 运行 testNG 测试
【发布时间】:2018-12-20 18:40:16
【问题描述】:

我有一个使用 testng API 动态创建 testng.xml 文件的项目,因此使用 java 的主要功能,我的项目按预期工作,但是如果我使用 'Run as->maven build' 通过 maven 运行我的项目看起来只有 BeforeSuite 和 BeforeTest 类正在运行,但实际测试没有运行。这是我的 pom.xml 文件:

<build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
          <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-testng</artifactId>
      <version>2.22.1</version>
    </dependency>
  </dependencies>
          <configuration>
          <forkMode>never</forkMode>
          </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.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</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>

这是我的 MainRunner.java 类:

package main;

import java.io.IOException;

import org.testng.TestNG;

import framework.CreateXml;

public class MainRunner {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        TestNG testng= new TestNG();
        CreateXml createAndRun = new  CreateXml(testng);
        createAndRun.createAndRun();

    }

}

此 MainRunner 类调用创建 testng.xml 套件并运行测试的“createAndRun()”函数,如下所示:

testng.setXmlSuites(mySuites);

        testng.setListenerClasses(listenerClasses);
        testng.setVerbose(3);
        testng.run();

最后,这是堆栈跟踪:

[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ Automata ---
[WARNING] The parameter forkMode is deprecated since version 2.14. Use forkCount and reuseForks instead.
[WARNING] useSystemClassloader setting has no effect when not forking
[INFO] Surefire report directory: D:\Git\automata\Automata\target\surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.testng.TestNGProvider
[INFO] Running TestSuite
Configuring TestNG with: TestNG60Configurator
Surefire suite started.....
In Before Test
Starting ChromeDriver 2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90) on port 45113
Only local connections are allowed.
Dec 20, 2018 11:57:27 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: 8.472 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ Automata ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ Automata ---
[INFO] Installing D:\Git\automata\Automata\target\Automata-0.0.1-SNAPSHOT.jar to C:\Users\A\.m2\repository\com\sanrotech\com\Automata\0.0.1-SNAPSHOT\Automata-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Git\automata\Automata\pom.xml to C:\Users\A\.m2\repository\com\sanrotech\com\Automata\0.0.1-SNAPSHOT\Automata-0.0.1-SNAPSHOT.pom
[INFO] 
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ Automata >>>
[INFO] 
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ Automata <<<
[INFO] 
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ Automata ---
[WARNING] 
java.lang.ClassNotFoundException: main.MainRunner
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
    at java.lang.Thread.run(Thread.java:748)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.582 s
[INFO] Finished at: 2018-12-20T23:57:30+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project Automata: An exception occured while executing the Java class. main.MainRunner -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project Automata: An exception occured while executing the Java class. main.MainRunner
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:194)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. main.MainRunner
    at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:352)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 20 more
Caused by: java.lang.ClassNotFoundException: main.MainRunner
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
    at java.lang.Thread.run(Thread.java:748)
[ERROR] 
[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

我想知道我在这里错过了什么。

【问题讨论】:

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


    【解决方案1】:

    将此添加到您的 Pom.xml 文件中,在您放置 testng 源位置的位置 src/main/javasrc/test/javasrc/test/resources

     <configuration>
     <suiteXmlFiles>
     <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
     </suiteXmlFiles>
     </configuration>
    

    【讨论】:

    • 我没有 testng.xml 文件,它是在运行时创建的,所以不知道如何处理
    • 您在创建 testng.xml 文件时犯了错误。等我发一下。
    • 实际上我的 testng.xml 文件是根据 excel 表中的值动态创建的。因此,一旦创建了 testng.xml 文件,我就使用“testng.run()”运行它。当我将它作为 java 应用程序运行时它可以工作,但在通过 maven 运行时会产生问题。
    • 其实我理解你在说什么。我以为你已经通过编写 java 代码来创建 testng.xml 来生成它。你试过吗?
    • 是的 testng.xml 是由 java 代码在内部生成的,如果我们作为 java 应用程序运行,项目可以工作,唯一的问题是 maven。试图弄清楚。
    【解决方案2】:

    终于找到了解决办法,只需要补充一下

    <classpathScope>test</classpathScope>
    

    到 exec 插件的配置,现在它按预期工作。

    【讨论】:

    • 您可能希望接受您的回答,以便问题结束。
    • 你忘了说,应该在哪里设置类路径范围。没有那个,你的答案是没有用的。编辑,我将删除反对票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    相关资源
    最近更新 更多