使用`mvn test`参数化JUnit测试是否正确?

时间:2013-03-01 14:16:54

标签: java maven junit

我刚刚使用JUnit 4.11实现了一个JUnit测试用例:

https://github.com/junit-team/junit/blob/master/doc/ReleaseNotes4.11.md#example-1

我使用

创建了一个maven项目
<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yyt</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>         
    </dependencies>

</project>

这个测试用例:

import java.util.Arrays;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class Example {

    @Parameters(name = "{index}: fib({0})={1}")
      public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
          { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
      }

    private int input;
    private int expected;

    public Example(int input, int expected) {
      this.input = input;
      this.expected = expected;
    }

    @Test
      public void test() {
      }
}

但是当我使用mvn test进行测试时,maven说:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

如何让它发挥作用?

4 个答案:

答案 0 :(得分:10)

问题是maven的命名约定基于maven-surefire-plugin 需要命名,如 Test.java 测试 .java TestCase * .java 用于单元测试。对于集成测试,maven-failsafe-plugin负责命名约定 IT * .java * IT.java * ITCase.java

答案 1 :(得分:1)

@khmarbaise 在这种情况下钉了它,但是......

有时你冒险在这里,因为

  • Maven和Eclipse都告诉你,你班上没有考试
  • 显然做;并且名字很好。
  • 这两个工具似乎找到正确的类并尝试在那里执行测试
  • 你开始怀疑Maven和Parameterized是否一起工作,这个问题的标题听起来很有希望

如果满足上面列出的相关条件,您的Maven输出应该沿着这些行说明:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running your.maven.properly.named.SomeTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.684 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)

如果确实如此,我的建议是:尝试查看您使用什么来获取数据实际上返回任何内容。 换句话说:看看用@Parameters注释的方法。没有数据,您的测试将无法运行。

答案 2 :(得分:0)

遇到了同样的问题。在junit https://github.com/junit-team/junit4/issues/664中找到了这个(仍然)未解决的问题。所以,现在没办法让它发挥作用。

答案 3 :(得分:0)

我知道的唯一解决方案 - 是运行整个班级,而不是在课堂上进行测试。

有效的前例:

mvn clean -Dtest=ExampleClassTest test

ex 不起作用:

mvn clean -Dtest=ExampleClassTest#test test

但是要小心,类中的所有测试都会带参数运行!

相关问题