TestNG是否支持JUnit4的@Rule之类的东西? [英] Does TestNG support something like JUnit4's @Rule?

查看:190
本文介绍了TestNG是否支持JUnit4的@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屋!

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