TestNG:如何跳过某些测试的 @BeforeMethod 调用

Nar*_*rma 7 java testng unit-testing

我正在使用 TestNG 进行单元测试。我正在使用@BeforeMethod保存记录,然后执行更新、搜索、删除测试。

我正在寻找可以避免对某些测试用例执行@BeforeMethod方法调用的选项。例如,我有三个测试保存、更新和删除。在这种情况下,我只想调用@BeforeMethod进行更新和删除测试,而不是保存测试。任何想法?

请注意,我不想使用@DependesOnMethods,因为不建议使用它。

提前致谢。

我的测试类如下所示:

@ContextConfiguration("file:application-context-test.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class MyEntityTest {

    int long myEntityId; 

    @BeforeMethod 
    public saveRecord(){
      session=getCurrentSessionFactory()
      myEntity = new myEntity();
      myEntityId = session.save(myEntity)
    }

    @Test
    public saveTest(){
      session=getCurrentSession()
      myEntity =session.getByID(myEntityId)
      session.save(myEntity)
    }

    @Test
    public updateTest(){
      session=getCurrentSession()
      myEntity =session.getByID(myEntityId)
      session.update(myEntity)
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

您可以在测试中进行分组,它将为不同的组调用单独的 beforeMethod http://testng.org/doc/documentation-main.html#test-groups