[英]Running a single test in maven -> No tests were executed
When I run a single test in Maven with this command:当我使用此命令在 Maven 中运行单个测试时:
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.在运行测试之前,我尝试运行
mvn clean
几次,但没有帮助。
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:这是通过将此依赖项添加到 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.这很奇怪。
You are probably picking up JUnit3 on your classpath somewhere, which effectively disables JUnit4.您可能在某个地方的类路径中选择了 JUnit3,这实际上禁用了 JUnit4。
Run mvn dependency:tree
to find out where it's coming in from and add exclude if from the dependency.运行
mvn dependency:tree
以找出它的来源并添加 exclude if 从依赖项。
也许您看到了这个 bug,据说它会影响 surefire 2.12 但不会影响 2.11?
I had the same problem.我有同样的问题。 It was caused by testng dependency that came with junit3.
这是由 junit3 附带的 testng 依赖引起的。 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>
我已将“maven-surefire-plugin”更改为 2.14.1 版本(从 2.12 开始),它有所帮助
I got this error when trying to use @org.junit.Test
尝试使用
@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
要使用的正确注解是
@org.junit.jupiter.api.Test
In my case, I was running a single test using mvn test -Dtest=MyTest.就我而言,我正在使用 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.
我的错误是唯一的测试将其 @test 注释注释掉了,所以 junit 在文件中没有找到任何测试。 Doh!
嗬!
从 2.6 更改为 2.18.1,现在一切正常
In the build session of the pom.xml, include this:在 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>
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然后从测试包的 pom 所在的目录运行测试命令
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.
例如在我的 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>
So when I run the maven test, I set it to false因此,当我运行 maven 测试时,我将其设置为 false
mvn test -Dtest=TestUserUpdate* -DskipTests=false
将 org.apache.maven.plugins:maven-surefire-plugin 更新为 2.22.0,已解决。
Had a similar problem adding jtestr dependency.添加 jtestr 依赖项时遇到类似问题。 It turns out one of its dependencies was picking up junit-3.8.1.
事实证明,它的依赖项之一是使用 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>
Try running maven in debug mode.尝试在调试模式下运行 maven。 It might give you more information.
它可能会为您提供更多信息。
mvn -X -Dtest=InitiateTest test
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.问题是使用
@Test
注释private
方法,导致它被忽略,更改为public
修复了问题。 :facepalm: :facepalm:
@Test
public void testImportOrderItems() {
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.我的建议是添加另一个参数,
-Dmaven.test.failure.ignore=true -DfailIfNoTests=false
可以解决它。
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.也许和我上次的尝试一样没用,但我刚刚阅读了一个 JUnit 4 测试类应该导入 org.junit.Test.*和 org.junit.Assert.*来考虑。 As you don't have the Assert import, it might be worth trying this quickly just to be sure...
由于您没有 Assert 导入,因此可能值得快速尝试一下以确保...
I don't really how the @Test annotation processes your test, but can you try prefixing your test method with "test"?我真的不知道@Test 注释如何处理您的测试,但是您可以尝试在您的测试方法前加上“test”吗?
public void testInit() throws Exception {
driver.get("http://localhost:8080/login.jsp");
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.