繁体   English   中英

在 maven 中运行单个测试 -> 未执行任何测试

[英]Running a single test in maven -> No tests were executed

当我使用此命令在 Maven 中运行单个测试时:

mvn test -Dtest=InitiateTest

我得到以下结果:

No tests were executed!

它在几分钟前工作,但现在由于某种原因停止工作。 在运行测试之前,我尝试运行mvn clean几次,但没有帮助。

测试看起来像这样:

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class InitiateTest {

    public static FirefoxDriver driver;

    @Before
        public void setUp() throws Exception {
            driver = new FirefoxDriver();
    }

    @Test
    public void initiateTest() throws Exception {
            driver.get("http://localhost:8080/login.jsp");
            ...
    }

    @After
    public void tearDown() throws Exception {
        driver.close();
    }
}

更新:

这是通过将此依赖项添加到 POM 引起的:

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium</artifactId>
   <version>2.0b1</version>
   <scope>test</scope>
</dependency>

当我删除它时,一切正常。 即使我添加这两个依赖项而不是前一个依赖项,一切正常:

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-support</artifactId>
   <version>2.0b1</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-firefox-driver</artifactId>
   <version>2.0b1</version>
   <scope>test</scope>
</dependency>

这很奇怪。

您可能在某个地方的类路径中选择了 JUnit3,这实际上禁用了 JUnit4。

运行mvn dependency:tree以找出它的来源并添加 exclude if 从依赖项。

也许您看到了这个 bug,据说它会影响 surefire 2.12 但不会影响 2.11?

我有同样的问题。 这是由 junit3 附带的 testng 依赖引起的。 只需为其添加一个排除语句,测试就可以工作了。

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium</artifactId>
  <version>2.0b1</version>
  <exclusions>
    <exclusion>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
    </exclusion>
  </exclusions>
</dependency>

我已将“maven-surefire-plugin”更改为 2.14.1 版本(从 2.12 开始),它有所帮助

尝试使用@org.junit.Test时出现此错误

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

要使用的正确注解是@org.junit.jupiter.api.Test

就我而言,我正在使用 mvn test -Dtest=MyTest 运行单个测试。 我的错误是唯一的测试将其 @test 注释注释掉了,所以 junit 在文件中没有找到任何测试。 嗬!

从 2.6 更改为 2.18.1,现在一切正常

在 pom.xml 的构建会话中,包括以下内容:

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

我有一个类似的问题。 所以我不得不使用从项目的根级别构建项目

mvn clean install -DskipTests=True

然后从测试包的 pom 所在的目录运行测试命令

mvn test -Dtest=TestClass

还要确保跳过选项的值为真。 例如在我的 pom 文件中,skip 的默认值为 true。

 <properties>
    <skipTests>true</skipTests>
</properties>


<build>
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skip>${skipTests}</skip>
            </configuration>
    </plugin>
</build>

因此,当我运行 maven 测试时,我将其设置为 false

mvn test -Dtest=TestUserUpdate* -DskipTests=false

将 org.apache.maven.plugins:maven-surefire-plugin 更新为 2.22.0,已解决。

添加 jtestr 依赖项时遇到类似问题。 事实证明,它的依赖项之一是使用 junit-3.8.1。 我使用下面的排除语句解决了它

<dependency>
  <groupId>org.jtestr</groupId>
  <artifactId>jtestr</artifactId>
  <exclusions>
   <exclusion>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
   </exclusion>
  </exclusions>
  <version>0.6</version>
  <scope>test</scope>
</dependency> 

尝试在调试模式下运行 maven。 它可能会为您提供更多信息。

mvn -X -Dtest=InitiateTest test

从不同的类复制和重构测试时,我遇到了这个问题。

问题是使用@Test注释private方法,导致它被忽略,更改为public修复了问题。 :facepalm:

    @Test
    public void testImportOrderItems() {
mvn test -Dtest='xxxx.*Test' -Dmaven.test.failure.ignore=true  -DfailIfNoTests=false

我遇到了同样的问题,没有执行任何测试! 我的建议是添加另一个参数, -Dmaven.test.failure.ignore=true -DfailIfNoTests=false可以解决它。

也许和我上次的尝试一样没用,但我刚刚阅读了一个 JUnit 4 测试类应该导入 org.junit.Test.*和 org.junit.Assert.*来考虑。 由于您没有 Assert 导入,因此可能值得快速尝试一下以确保...

我真的不知道@Test 注释如何处理您的测试,但是您可以尝试在您的测试方法前加上“test”吗?

public void testInit() throws Exception {
      driver.get("http://localhost:8080/login.jsp");
      ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM