【问题标题】:Running a single test in maven -> No tests were executed在 maven 中运行单个测试 -> 未执行任何测试
【发布时间】:2011-06-06 11:17:59
【问题描述】:

当我使用此命令在 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>

这很奇怪。

【问题讨论】:

  • 您要运行什么样的测试?您没有添加@Ignore 吗?
  • 可能不太有帮助..但请记住,这两个都是测试版产品,并且很容易被破坏。

标签: java testing selenium maven functional-testing


【解决方案1】:

也许您看到this bug, 据说会影响surefire 2.12 但不会影响2.11?

【讨论】:

  • 谢谢,这就是我遇到的问题。我最终使用了包含修复程序的 2.12.3。
  • 其次,2.12.3 包含对此的修复。
  • 显然 2.19.1 也带有 bug :(
  • 它对我也有帮助,确实是由 testng 与 selenium 内部版本冲突引起的。谢谢 。拿来用surefire 2.19.1,现在没问题了。
  • 链接已失效,答案中没有总结,所以这个答案现在几乎毫无意义。
【解决方案2】:

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

运行 mvn dependency:tree 以找出它的来源并向依赖项添加排除项。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。这是由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>
    

    【讨论】:

    • 每走一分钟对我来说都是一样的......或者在我发现这个之前一个小时:/ THX man
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      我在尝试使用 @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

      【讨论】:

      • 哇!根本没想到这一点,是的,自动导入修复使导入 org.junit.Test 变得容易 - 这个 Jupiter API 到底是什么? ://
      • 完全相同的问题。拯救了我的一天!
      【解决方案6】:

      在添加 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> 
      

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          在 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>
          

          【讨论】:

          • 确实我的 pom.xml 完全缺少 maven-surefire-plugin 条目,所以我跳过了关于插件版本的其他建议——我的错
          【解决方案9】:

          我遇到了类似的问题。所以我不得不从项目的根级别构建项目使用

          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
          

          【讨论】:

            【解决方案10】:

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

            【讨论】:

              【解决方案11】:

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

              【讨论】:

              • 欢迎来到 StackOverflow。请注意这种类型的答案应该在评论中。
              【解决方案12】:

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

              mvn -X -Dtest=InitiateTest test
              

              【讨论】:

                【解决方案13】:

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

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

                    @Test
                    public void testImportOrderItems() {
                

                【讨论】:

                  【解决方案14】:

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

                  【讨论】:

                    【解决方案15】:
                    mvn test -Dtest='xxxx.*Test' -Dmaven.test.failure.ignore=true  -DfailIfNoTests=false
                    

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

                    【讨论】:

                    • 确保错误不会被忽略绝对不会阻止错误的发生。
                    【解决方案16】:

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

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

                    【讨论】:

                    • @Test 注解是在 jUnit 4 中引入的,在 jUnit 3 中每个方法都必须以“test”开头
                    • 是的,不幸的是这并没有帮助。 @Test 注解就够了。
                    猜你喜欢
                    • 2019-12-06
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2016-12-06
                    • 1970-01-01
                    • 1970-01-01
                    • 2020-03-18
                    • 2013-08-09
                    相关资源
                    最近更新 更多