一次测试即可获得多个结果 [英] Junit multiple results in one test

查看:58
本文介绍了一次测试即可获得多个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道这被认为是反模式,我当然愿意采取更好的方法.

Ok, I know this is considered an anti-pattern, and I am certainly open to a better way of doing this.

我有一个枚举值映射.我想确保将每个枚举值都分配给某个对象.我的测试看起来像这样.

I have a map of enum values. I want to ensure that each of those enum values is assigned to something. My test looks like this.

@Test
public void eachRowRequiresCellCalc()
{
    Model model = new Model();
    EnumValues[] values = EnumValues.values();
    for (EnumValues value : values)
    {
        Assert.assertTrue(String.format("%s must be assigned", value.name()), model.hasEnumValue(value));
    }
}

这可以完成我要寻找的90%的内容.它没有做的是告诉我是否未分配多个枚举值(第一个失败).JUnit是否可以通过每次测试使多个失败?

This works and accomplishes 90% of what I'm looking for. What it doesn't do is show me if multiple enum values are unassigned (it fails on the first). Is there a way with JUnit to have multiple fails per test?

推荐答案

使用 junit-快速检查为:

@RunWith(Theories.class)
public class Models {
    @Theory public void mustHaveValue(@ForAll @ValuesOf EnumValues e) {
        assertTrue(e.name(), new Model().hasEnumValue(e));
    }
}

这将为您的枚举的每个值运行理论方法.

This would run the theory method for every value of your enum.

另一种表达方式是通过参数化测试.

Another way to express this would be via a parameterized test.

(完全公开:我是junit-quickcheck的创建者.)

(Full disclosure: I am the creator of junit-quickcheck.)

这篇关于一次测试即可获得多个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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