JUnit理论问题

发布于 2024-09-25 07:18:32 字数 865 浏览 6 评论 0原文

我正在编写一个测试用例,其中我想为一个测试用例运行一个数据点,为第二个测试用例运行第二个数据点。

@RunWith(Theories.class)
public class DummyTest {

    @DataPoints
    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}

我希望对于第一个测试用例,我的第一个数据点(即 getFileNames 方法)被调用,对于第二个测试用例,应该调用 getSecondFileNames 数据点。有人能建议这可行吗?

谢谢,
谢卡尔

I am writing a test case where in I want to run a one DataPoint for one test case and second DataPoint for second test case.

@RunWith(Theories.class)
public class DummyTest {

    @DataPoints
    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}

I want that for first test case my first datapoints i.e. getFileNames method is called and for second test case getSecondFileNames datapoints should be called. Can anybody suggest is this feasible?

Thanks,
Shekhar

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

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

发布评论

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

评论(4

被你宠の有点坏 2024-10-02 07:18:32

从即将推出的 JUnit 4.12 开始,您现在可以命名数据点集并要求参数仅来自该集,例如:

@RunWith(Theories.class)
public class DummyTest {

    @DataPoints("fileNames1")
    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(@FromDataPoints("fileNames1") String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints("fileNames2")
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(@FromDataPoints("fileNames2") String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}

这应该完全解决您的问题:-)。

As of the shortly-upcoming JUnit 4.12, you can now name sets of datapoints and require parameters to come only from that set, e.g.:

@RunWith(Theories.class)
public class DummyTest {

    @DataPoints("fileNames1")
    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(@FromDataPoints("fileNames1") String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints("fileNames2")
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(@FromDataPoints("fileNames2") String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}

This should exactly solve your problem :-).

讽刺将军 2024-10-02 07:18:32

您可以实现自己的 ParameterSupplier,如下所述: http://blog.schauderhaft.de/2010/02/07/junit-theories/

You could implement your own ParameterSupplier, as explained here: http://blog.schauderhaft.de/2010/02/07/junit-theories/

兮子 2024-10-02 07:18:32

测试可以分为“固定装置”,其中固定装置是一组共享相同设置的代码。将使用相同数据点的案例的测试放在同一个类中,这样每组数据就有一个类。

Tests can be grouped into "fixtures", where a fixture is a set of code that shares the same setup. Put the tests for cases using the same datapoints together in the same class, so you have one class for each set of data.

和我恋爱吧 2024-10-02 07:18:32

您看过 JUnitParams 吗?如果您只想将参数传递给方法(这就是您的示例所显示的全部内容),那么这是一个更简洁的选择。

@RunWith(JUnitParamsRunner.class)
public class DummyTest {

    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}

Have you looked at JUnitParams? If you just want to pass parameters to methods, which is all that your example shows, it's a much cleaner option.

@RunWith(JUnitParamsRunner.class)
public class DummyTest {

    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

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