我有一个maven程序,它编译得很好 . 当我运行 mvn test
时,它不会运行任何测试(在TESTs Headers 下说 There are no tests to run.
) .
我用一个超级简单的设置重新创建了这个问题,我将在下面包含以及使用 -X
运行时的输出 .
单元测试从eclipse运行良好(两者都有默认的junit包,当我改为包含maven下载的junit.jar时) . mvn test-compile
也正确地在test-classes下创建了类 . 我在OSX 10.6.7上使用Maven 3.0.2和java 1.6.0_24运行它 .
这是目录结构:
/my_program/pom.xml
/my_program/src/main/java/ClassUnderTest.java
/my_program/src/test/java/ClassUnderTestTests.java
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my_group</groupId>
<artifactId>my_program</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>My Program</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
ClassUnderTest.java:
public class ClassUnderTest {
public int functionUnderTest(int n) {
return n;
}
}
ClassUnderTestTests.java:
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ClassUnderTestTests {
private ClassUnderTest o;
@Before
public void setUp() {
o = new ClassUnderTest();
}
@Test
public void testFunctionUnderTest_testCase1() {
Assert.assertEquals(1, o.functionUnderTest(1));
}
@Test
public void testFunctionUnderTest_testCase2() {
Assert.assertEquals(2, o.functionUnderTest(2));
}
}
End of mvn -X test:
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-surefire-plugin:2.7.1, parent: sun.misc.Launcher$AppClassLoader@5224ee]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test' with basic configurator -->
[DEBUG] (s) basedir = /Users/aaron/Programs/my_program
[DEBUG] (s) childDelegation = false
[DEBUG] (s) classesDirectory = /Users/aaron/Programs/my_program/target/classes
[DEBUG] (s) disableXmlReport = false
[DEBUG] (s) enableAssertions = true
[DEBUG] (s) forkMode = once
[DEBUG] (s) junitArtifactName = junit:junit
[DEBUG] (s) localRepository = id: local
url: file:///Users/aaron/.m2/repository/
layout: none
[DEBUG] (f) parallelMavenExecution = false
[DEBUG] (s) pluginArtifactMap = {org.apache.maven.plugins:maven-surefire-plugin=org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.7.1:, org.apache.maven.surefire:surefire-booter=org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile, org.apache.maven.surefire:surefire-api=org.apache.maven.surefire:surefire-api:jar:2.7.1:compile, org.apache.maven.surefire:maven-surefire-common=org.apache.maven.surefire:maven-surefire-common:jar:2.7.1:compile, org.apache.maven.shared:maven-common-artifact-filters=org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:2.0.5:compile, junit:junit=junit:junit:jar:3.8.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile}
[DEBUG] (s) printSummary = true
[DEBUG] (s) project = MavenProject: my_group:my_program:1.0-SNAPSHOT @ /Users/aaron/Programs/my_program/pom.xml
[DEBUG] (s) projectArtifactMap = {junit:junit=junit:junit:jar:4.8.1:test}
[DEBUG] (s) redirectTestOutputToFile = false
[DEBUG] (s) remoteRepositories = [ id: central
url: http://repo1.maven.org/maven2
layout: default
snapshots: [enabled => false, update => daily]
releases: [enabled => true, update => never]
]
[DEBUG] (s) reportFormat = brief
[DEBUG] (s) reportsDirectory = /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] (s) session = org.apache.maven.execution.MavenSession@dfbb43
[DEBUG] (s) skip = false
[DEBUG] (s) skipTests = false
[DEBUG] (s) testClassesDirectory = /Users/aaron/Programs/my_program/target/test-classes
[DEBUG] (s) testFailureIgnore = false
[DEBUG] (s) testNGArtifactName = org.testng:testng
[DEBUG] (s) testSourceDirectory = /Users/aaron/Programs/my_program/src/test/java
[DEBUG] (s) trimStackTrace = true
[DEBUG] (s) useFile = true
[DEBUG] (s) useManifestOnlyJar = true
[DEBUG] (s) workingDirectory = /Users/aaron/Programs/my_program
[DEBUG] -- end configuration --
[INFO] Surefire report directory: /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] Setting system property [user.dir]=[/Users/aaron/Programs/my_program]
[DEBUG] Setting system property [localRepository]=[/Users/aaron/.m2/repository]
[DEBUG] Setting system property [basedir]=[/Users/aaron/Programs/my_program]
[DEBUG] Using JVM: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-junit4:jar:2.7.1:test (selected for test)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:test (selected for test)
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.7.1/surefire-junit4-2.7.1.jar Scope: test
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: test
[DEBUG] Test Classpath :
[DEBUG] /Users/aaron/Programs/my_program/target/test-classes
[DEBUG] /Users/aaron/Programs/my_program/target/classes
[DEBUG] /Users/aaron/.m2/repository/junit/junit/4.8.1/junit-4.8.1.jar
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
Forking command line: /bin/sh -c cd /Users/aaron/Programs/my_program && /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar /Users/aaron/Programs/my_program/target/surefire/surefirebooter6118081963679415631.jar /Users/aaron/Programs/my_program/target/surefire/surefire4887918564882595612tmp /Users/aaron/Programs/my_program/target/surefire/surefire9012255138269731406tmp
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.089s
[INFO] Finished at: Mon May 30 12:03:09 EDT 2011
[INFO] Final Memory: 7M/62M
[INFO] ------------------------------------------------------------------------
21 回答
如果模块的包装没有正确声明,另一件事可能导致Maven找不到测试 .
在最近的一个案例中,有人有
<packaging>pom</packaging>
,我的测试从未运行过 . 我将其更改为<packaging>jar</packaging>
,现在工作正常 .如果使用的JUnit不是标准(
junit:junit
),例如......也可能是junitArtifactName
还有一个提示(除了之前的答案):
在Eclipse中,转到项目的Properties> click
Run/Debug Settings
:在那里,您可以添加(新...)或删除(删除)项目中的任何JU(JUnit)测试(在
src/test/java
文件夹或课程下) .如果您使用“Abstract”为测试添加前缀,则默认情况下它将被忽略 .
在我的情况下,它添加了junit-vintage-engine,使其与旧版本的JUnit测试兼容,并且可以运行它们 . 因为我正在使用JUnit 5 .
检查一下(对于jUnit - 4.12和Eclipse surefire插件)
在依赖项中的POM.xml中添加所需的jUnit版本 . Do Maven - >更新项目以查看项目中导出的必需jar .
测试类位于文件夹src / test / java和该文件夹的子目录下(或者可以在config testSourceDirectory中的POM中指定基本文件夹) . 该类的名称应该有tailng字'Test' .
测试类中的测试方法应该有@Test注释
另外,检查您的测试类目录(例如src / test / java)是否对应于
<build>
属性下pom.xml中属性<testSourceDirectory>
中列出的目录 . 花了一段时间才发现这一点 .如果您已在JUnit 4中编写测试并将JUnit 5依赖项添加到surefire插件,那么您的测试将无法运行 .
在这种情况下,只需从surefire插件中评论JUnit 5依赖项:
我遇到了同样的问题,它通过pom.xml中的以下更改来解决:
...
变成:
当使用JUnit 5时,我遇到了同样的问题 . Maven Surefire需要一个插件来运行JUnit 5测试 . 将其添加到我们的
pom.xml
:资料来源:https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven
我用过这段代码
到我的pom.xml,只需确保具体的testng文件
我还发现单元测试代码应放在测试文件夹下,如果将它放在主文件夹下,则无法将其识别为测试类 . 例如 .
Wrong
Right
没有运行测试用例的另一个原因发生在我身上 - 我有一个名为“test”的属性用于完全不同的目的,但它干扰了surefire插件 . 因此,请检查您的POM:
并删除它 .
如果有人搜索过我并且我没有解决它,我有一个用于不同测试的库:
当我安装junit一切正常时,我希望并帮助这个:
这是我必须添加到我的pom.xml的确切代码:
这是我的依赖:
我也有类似的问题,经过探索发现testng依赖导致了这个问题 . 从pom中删除了testng依赖(因为我不再需要它),它开始对我很好 .
如果您的测试类名称不符合标准命名约定(由上面的@axtavt突出显示),您需要在
pom.xml
中添加模式/类名称以便Maven选择测试 -默认情况下,Maven在查找要运行的测试时使用以下命名约定:
Test*
*Test
*TestCase
您的测试类不遵循这些约定 . 您应该重命名它或configure Maven Surefire Plugin以使用其他模式进行测试类 .
如果你有一个共享的Java / Groovy应用程序,而你所拥有的只是Groovy单元测试,那么Maven将找不到任何测试 . 这可以通过在src / test / java下添加一个单元测试来解决 .
应该
Maven找到那些结束测试或以Test开始自动运行 .
但是,你可以使用
运行测试 .
如果项目有
<packaging>pom</packaging>
,Maven将不会运行您的测试您需要将包装设置为jar(或其他一些java artefact类型)要运行的测试:
<packaging>jar</packaging>