Eclipse Testrunner 中具有名称的 ParameterizedTest
当您使用 Eclipse TestRunner 运行 JUnit 4 ParameterizedTest 时,图形表示相当愚蠢:对于每个测试,您都有一个名为 [0]
、[1]
等的节点。 是否可以给测试 [0]
、[1]
等明确的名称? 为测试实现 toString 方法似乎没有帮助。
When you run a JUnit 4 ParameterizedTest with the Eclipse TestRunner, the graphical representation is rather dumb: for each test you have a node called [0]
, [1]
, etc.
Is it possible give the tests [0]
, [1]
, etc. explicit names? Implementing a toString
method for the tests does not seem to help.
(This is a follow-up question to JUnit test with dynamic number of tests.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为 jUnit 4 中没有内置任何东西可以做到这一点。
我已经实施了一个解决方案。 我已经基于现有的类构建了自己的
参数化
类:使用方式如下:
这意味着我之前实例化了测试类。 我希望这不会导致任何错误...我想我应该测试一下测试:)
I think there's nothing built in in jUnit 4 to do this.
I've implemented a solution. I've built my own
Parameterized
class based on the existing one:To be used like:
This implies that I instantiate the test class earlier. I hope this won't cause any errors ... I guess I should test the tests :)
JUnit4 现在允许为参数化注释指定名称属性,以便您可以从参数的 index 和 toString 方法指定命名模式。 例如:
JUnit4 now allows specifying a name attribute to the Parameterized annotation, such that you can specify a naming pattern from the index and toString methods of the arguments. E.g.:
一种无代码但不太舒服的解决方案是传递足够的上下文信息来识别断言消息中的测试。 您仍然会看到 testXY[0] 失败,但详细消息会告诉您那是哪一个。
A code-less though not that comfortable solution is to pass enough context information to identify the test in assert messages. You will still see just testXY[0] failed but the detailed message tells you which one was that.
如果您使用 JUnitParams 库 (因为我有 此处描述),参数化测试将其字符串化参数作为其自己的默认测试名称。
此外,你可以在他们的示例中看到,JUnitParams 还允许您使用
@TestCaseName
拥有自定义测试名称:If you use JUnitParams library (as I have described here), the parameterized tests will have their stringified parameters as their own default test names.
Moreover, you can see in their samples, that JUnitParams also allows you to have a custom test name by using
@TestCaseName
:没有任何迹象表明该功能已经或将要实现。 我会请求这个功能,因为拥有它很好。
There's no hint that this feature is or will be implemented. I would request this feature because it's nice to have.