7

我不想为我的测试用例不断创建相同的调试配置,而是希望能够简单地保存一些在我所有的 Junit 测试中通用的参数,右键单击一个特定的测试,然后运行那个单一的运行配置。IE我想要一个可以将当前选择的测试用例作为参数的单一调试配置,而不是要求我每次在 JUnit 运行配置中手动指定它。我在对话框中的唯一选项似乎是指定单个测试类或运行项目中的所有测试。结果,Eclipse 为我的所有测试用例提供了数十种运行配置。

而不是指定一个特定的测试类,我希望它指定一个变量,如 ${container_loc} 或 ${resource_loc} 以便类在这个问题中运行。Eclipse 中是否有一个变量可以指定我可以放置在对话框的测试类字段中的当前选定的 Java 类?

这很有用的一个具体示例是在运行 Lucene 单元测试时。您可以指定许多参数来自定义测试,其中一些-ea是必需的。每次我想在 Eclipse 中测试 Lucene 中的特定测试用例时,我必须在 Eclipse 调试配置对话框中手动设置这些变量:-/。

4

1 回答 1

1

你看过 JUnit 中的参数化测试吗?这是一个例子:

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 ParamTest {
    @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 ParamTest(int input, int expected) {
        this.input = input;
        this.expected = expected;
    }

    @Test
    public void test() {
        Assert.assertEquals(expected, input);
    }
}

如果您只想一次运行一个测试,您可以使用私有变量,如下所示:

    public class MultipleTest {
    private int x;
    private int y;

    public void test1(){
        Assert.assertEquals(x, y);
    }
    public void test2(){
        Assert.assertTrue(x  >y);
    }

    public void args1(){
        x=10; y=1;
    }
    public void args2(){
        x=1;y=1;
    }
    public void args3(){
        x=1;y=10;
    }
    @Test
    public void testArgs11(){
        args1();
        test1();
    }
    @Test
    public void testArgs21(){
        args2();
        test1();
    }
    @Test
    public void testArgs31(){
        args3();
        test1();
    }
    @Test
    public void testArgs12(){
        args1();
        test2();
    }
    @Test
    public void testArgs22(){
        args2();
        test2();
    }
    @Test
    public void testArgs32(){
        args3();
        test2();
    }
}
于 2014-12-11T19:12:10.990 回答