TestNG 是否支持 JUnit4 的 @Rule 之类的东西?
TestNG 有类似 @Rule
的东西吗?我正在具体考虑:
@Rule public TemporaryFolder folder = ...
或者甚至
@Rule public MethodRule globalTimeout = new Timeout(20);
我知道我可以使用 setUp()
和 tearDown()
等效项手动实现这些东西,但这些并没有给我我使用@Rule 获得的便利。
Does TestNG have something like @Rule
? I am thinking specifically about:
@Rule public TemporaryFolder folder = ...
Or even
@Rule public MethodRule globalTimeout = new Timeout(20);
I am aware that I can manually implement these things using setUp()
and tearDown()
equivalents, but these don't give me the convenience that I get using @Rule
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
规则很容易模拟,例如使用超类:
如果扩展 Base,将始终自动创建一个临时目录,然后将其删除。
与规则相比,这种方法的优点是规则始终是类范围的,而使用 TestNG,您可以围绕方法、测试、类、组甚至套件来实现它们。
Rules are pretty easy to emulate, for example with super classes:
If you extend Base, a temporary directory will always be automatically created and then deleted.
The advantage of this approach over Rules is that Rules are always class scoped, while with TestNG, you can implement them around methods, tests, classes, groups and even suites.
TestNG的
BeforeClass
/AfterClass
可以模拟JUnit的rule
/ruleClass
之类的东西,但是有一些功能和这些类无法复制的效果,例如:重复、过滤等。但是,TestNG 提供了一些接口可以用来模拟这些功能,例如
IAnnotationTransformer
、IMethodInterceptor< /代码>。
BeforeClass
/AfterClass
of TestNG can simulate something like therule
/ruleClass
of JUnit, but there are some functions and effects that these classes cannot replicate, such as: repeat, filter, etc.However, there are some interfaces provided by TestNG that could be used to simulate these functions instead, e.g.
IAnnotationTransformer
,IMethodInterceptor
.