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

发布于 2024-10-11 12:48:55 字数 1728 浏览 10 评论 0原文

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

这很奇怪。

When I run a single test in Maven with this command:

mvn test -Dtest=InitiateTest

I'm getting the following result:

No tests were executed!

It worked a couple of minutes ago, but now it stopped working for some reason. I tried running mvn clean a couple of times before running the test, it doesn't help.

The test looks like this:

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();
    }
}

UPDATE:

It's caused by adding this dependency to POM:

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

When I remove it, everything works fine. Everything works fine even when I add these two dependencies instead of the previous one:

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

This is weird.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

铜锣湾横着走 2024-10-18 12:48:56

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

I have changed "maven-surefire-plugin" to 2.14.1 version (from 2.12) and it helped

旧城烟雨 2024-10-18 12:48:56

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

Had a similar problem adding jtestr dependency. It turns out one of its dependencies was picking up junit-3.8.1. I solved it using the exclusion statement below

<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> 
柠檬色的秋千 2024-10-18 12:48:56

就我而言,我使用 mvn test -Dtest=MyTest 运行单个测试。我的错误是唯一的测试已注释掉其 @test 注释,因此 junit 在文件中找不到测试。哎哟!

In my case, I was running a single test using mvn test -Dtest=MyTest. My mistake was that the only test had its @test annotation commented out so no test was being found in the file by junit. Doh!

鱼窥荷 2024-10-18 12:48:56

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

In the build session of the pom.xml, include this:

 <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>
近箐 2024-10-18 12:48:56

我有类似的问题。因此,我必须使用从项目的根级别构建项目

mvn clean install -DskipTests=True

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

mvn test -Dtest=TestClass

还要确保skip选项的值为真的。例如在我的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

I had a similar issue. So I had to build the project from project's root level using

mvn clean install -DskipTests=True

And then run the test command from the directory where test package's pom was residing

mvn test -Dtest=TestClass

Also make sure that value of skip option is true. For example in my pom file, the default value of skip is 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>

So when I run the maven test, I set it to false

mvn test -Dtest=TestUserUpdate* -DskipTests=false
柠北森屋 2024-10-18 12:48:56

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

changed from 2.6 to 2.18.1 and things work now

乖乖 2024-10-18 12:48:56

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

update the org.apache.maven.plugins:maven-surefire-plugin to 2.22.0, it was resolved.

叹倦 2024-10-18 12:48:56

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

mvn -X -Dtest=InitiateTest test

Try running maven in debug mode. It might give you more information.

mvn -X -Dtest=InitiateTest test
故乡的云 2024-10-18 12:48:56

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

该问题是用 @Test 注释 private 方法,导致它被忽略,更改为 public 解决了该问题。 :捂脸:

    @Test
    public void testImportOrderItems() {

I had this issue when duplicating and refactoring a test from a different class.

The issue was annotating a private method with @Test, causing it to be ignored, changing to public fixed the issue. :facepalm:

    @Test
    public void testImportOrderItems() {
弥枳 2024-10-18 12:48:56

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

Maybe as useless as my last attempt, but I just read a JUnit 4 test class should import org.junit.Test.* and org.junit.Assert.* to be considered so.
As you don't have the Assert import, it might be worth trying this quickly just to be sure...

木有鱼丸 2024-10-18 12:48:56
mvn test -Dtest='xxxx.*Test' -Dmaven.test.failure.ignore=true  -DfailIfNoTests=false

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

mvn test -Dtest='xxxx.*Test' -Dmaven.test.failure.ignore=true  -DfailIfNoTests=false

I have meet the same question that No tests were executed!
My suggestion is add another paramters that -Dmaven.test.failure.ignore=true -DfailIfNoTests=false can solve it.

ζ澈沫 2024-10-18 12:48:56

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

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

I don't really how the @Test annotation processes your test, but can you try prefixing your test method with "test"?

public void testInit() throws Exception {
      driver.get("http://localhost:8080/login.jsp");
      ...
}
姐不稀罕 2024-10-18 12:48:55

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

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

You are probably picking up JUnit3 on your classpath somewhere, which effectively disables JUnit4.

Run mvn dependency:tree to find out where it's coming in from and add exclude if from the dependency.

触ぅ动初心 2024-10-18 12:48:55

也许您正在看到此错误,据说该错误会影响surefire 2.12,但不会影响2.11?

Perhaps you are seeing this bug, which is said to affect surefire 2.12 but not 2.11?

绮烟 2024-10-18 12:48:55

我也有同样的问题。这是由于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>

I had the same problem. It was caused by testng dependency that came with junit3. Just add a exclusion statement for it and tests should work.

<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>
提赋 2024-10-18 12:48:55

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

I got this error when trying to use @org.junit.Test

with

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

The correct annotation to be used is @org.junit.jupiter.api.Test

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文