TestNG是否支持JUnit4的@Rule之类的东西? [英] Does TestNG support something like JUnit4's @Rule?
问题描述
TestNG是否具有类似@Rule
的东西?我正在特别考虑:
Does TestNG have something like @Rule
? I am thinking specifically about:
@Rule public TemporaryFolder folder = ...
甚至
@Rule public MethodRule globalTimeout = new Timeout(20);
我知道我可以使用setUp()
和tearDown()
等效项手动实现这些操作,但是这些并不能为我提供使用@Rule
所带来的便利.
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
.
推荐答案
规则非常容易模仿,例如使用超级类:
Rules are pretty easy to emulate, for example with super classes:
public void Base {
@BeforeMethod
public void createTempDir() { ... }
@AfterMethod
public void deleteTempDir() { ... }
}
public void MyTest extends Base {
@Test
...
}
如果扩展Base,将始终自动创建一个临时目录,然后将其删除.
If you extend Base, a temporary directory will always be automatically created and then deleted.
与Rule相比,此方法的优点是Rule始终在类范围内,而使用TestNG时,您可以在方法,测试,类,组甚至套件周围实现它们.
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是否支持JUnit4的@Rule之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!