Maven在运行testng测试用例时抛出错误

Maven在运行testng测试用例时抛出错误

本文介绍了Maven在运行testng测试用例时抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Eclipse + Maven + TestNG 并且我打算运行 Selenium 测试用例.

I have steup Eclipse + Maven + TestNG and I intend to run Selenium Test cases.

这是我的 POM 文件:

This is my POM File:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyGroupId</groupId>
<artifactId>TestSuite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.32.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-testng</artifactId>
        <version>2.14.1</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
</project>

现在,当我尝试运行 Maven 测试时,出现以下错误:

Now when I try to run Maven test, I get following error:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
org.apache.maven.surefire.util.SurefireReflectionException:
java.lang.reflect.InvocationTargetException; nested exception is
java.lang.reflect.InvocationTargetException: null
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:130)
at org.apache.maven.surefire.booter.SurefireReflector.instantiateProvider(SurefireReflector.java:247)
at org.apache.maven.surefire.booter.ProviderFactory.createProvider(ProviderFactory.java:81)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:171)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
 Caused by: java.lang.NoSuchMethodError: org.apache.maven.surefire.providerapi.ProviderParameters.getRunOrderCalculator()Lorg/apache/maven/surefire/util/RunOrderCalculator;
at org.apache.maven.surefire.testng.TestNGProvider.<init>(TestNGProvider.java:67)
... 10 more

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

有人能告诉我我在这里缺少什么吗.

Can someone suggest me what's that I am missing here.

提前致谢.

推荐答案

你必须定义 maven-surefire-plugin 在 pluginManagement 部分,如下所示:

You have to define the maven-surefire-plugin in the pluginManagement section like the following:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.14.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

要将 testng 与 maven-surefire-plugin 结合使用,您通常只需要添加 testng 作为依赖项即可.

To use testng in relationship with the maven-surefire-plugin you usually only need to add testng as a dependency nothing more.

进一步删除您提供的依赖项:

Furthermore remove the dependency you've given:

<dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-testng</artifactId>
    <version>2.14.1</version>
</dependency>

除上述之外,这看起来更像是集成测试,这是 maven-failsafe-plugin 的工作不是 的 maven-surefire-plugin.

Apart from the above this is looking more like an integration tests which is the job of maven-failsafe-plugin and not of maven-surefire-plugin.

<plugin>
  <artifactId>maven-failsafe-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
</plugin>

这意味着您的测试(大概是基于 selenium 依赖的集成测试)可以通过使用:

This means your tests (presumably integration tests based on the selenium dependency) can be run by using:

mvn verify

这篇关于Maven在运行testng测试用例时抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 16:17