TestNG - 特定方法的@BeforeMethod [英] TestNG - @BeforeMethod for specific methods

查看:41
本文介绍了TestNG - 特定方法的@BeforeMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 TestNG 的 Spring Test 来测试我们的 DAO,并且我想在某些方法之前运行特定的文本夹具脚本,允许在每个方法之后回滚修改,以便测试可以自由地执行任何操作夹具数据.

I'm using Spring Test with TestNG to test our DAOs, and I wanted to run a specific text fixture script before certain methods, allowing the modifications to be rolled back after every method so that the tests are free to do anything with the fixture data.

最初我认为 'groups' 适合它,但我已经意识到它们不适合这个(见这个问题:TestNG BeforeMethod with groups ).

Initially I thought that 'groups' would be fit for it, but I already realized they're not intended for that (see this question: TestNG BeforeMethod with groups ).

有没有办法将 @BeforeMethod 方法配置为仅在特定的 @Test 之前运行?我看到的唯一方法是解决方法:

Is there any way to configure a @BeforeMethod method to run only before specific @Tests? The only ways I see are workarounds:

  • 定义一个普通的setup方法并在每个@Test方法的开头调用;
  • @BeforeMethod 方法移动到一个新类(顶级或内部类),以及依赖它的所有方法.
  • Define an ordinary setup method and call at the beginning of every @Test method;
  • Move the @BeforeMethod method to a new class (top level or inner class), along with all methods that depend on it.

都不是理想的,我希望我的测试自然地分组和干净,而不是因为缺乏替代品而分开.

Neither is ideal, I'd like to keep my tests naturally grouped and clean, not split due to lack of alternatives.

推荐答案

您可以在 @BeforeMethod 中添加一个类型为java.lang.reflect.Method"的参数.然后,TestNG 将注入当前测试方法的反射信息,包括方法名称,您可以使用该名称进行切换.

You could add a parameter your @BeforeMethod with the type 'java.lang.reflect.Method'. TestNG will then inject the reflection information for the current test method including the method name, which you could use for switching.

如果你再添加一个'Object'参数,你也会得到测试方法的调用参数.

If you add another 'Object' parameter, you will also get the invocation parameters of the test method.

您都可以在 TestNG 文档的第 5.18.1 章中找到有关 TestNG 注释方法的所有可能参数.

You'all find all on possible parameters for TestNG-annotated methods in chapter 5.18.1 of the TestNG documentation.

这篇关于TestNG - 特定方法的@BeforeMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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