如何为参数化的junit运行指定标签 [英] How to specify label to a Parameterized junit run

查看:88
本文介绍了如何为参数化的junit运行指定标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Eclipse中,在junit测试类中使用Parameterized运行器时,每次运行都用数字(0、1等)标记

In Eclipse, while using the Parameterized runner in a junit test class, each run is noted by a number (0, 1, etc.)

有没有办法用适当的标签替换这个数字?

Is there a way to replace this number with a proper label?

PS::我使用的是 JUNIT 版本 4.8 ,该版本早于4.11,因此 @Parameters 不需要任何论点

PS: I am using a JUNIT version 4.8 older than 4.11 so the @Parameters does not take any argument

测试用例:

@RunWith(value = Parameterized.class)
public class TestClass {

    @Parameters
    public static Collection<Object[]> getLabels() {
        List<Object[]> labels = new ArrayList<Object[]>();
        labels.add(new Object[] {"Toto"});
        labels.add(new Object[] {"Titi"});
        return labels;
    }

    private final String label;

    public TestClass(String label) {
        this.label = label;
    }

    @Test
    public void test1() {
        assertTrue(true);
    }
}

结果:

推荐答案

有一种简单的方法可以轻松地识别参数化测试中的各个测试用例,您可以使用@Parameters提供一个名称批注.
允许该名称包含在运行时替换的占位符:

There is an easy way to easily identify the individual test cases in a Parameterized test, you may provide a name using the @Parameters annotation.
This name is allowed to contain placeholders that are replaced at runtime:

{index}:当前参数索引
{0},{1},…:第一个,第二个,依此类推,

{index}: the current parameter index
{0}, {1}, …: the first, second, and so on, parameter value

在此处查看示例: https://github.com/junit-team/junit/wiki/Parameterized-tests

这篇关于如何为参数化的junit运行指定标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆