一次测试即可获得多个结果

编程入门 行业动态 更新时间:2024-10-27 20:25:10
本文介绍了一次测试即可获得多个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.)

更多推荐

一次测试即可获得多个结果

本文发布于:2023-10-23 18:42:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521692.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!