通过 TestNG 进行依赖测试

Testing with dependencies via TestNG

我有这两个测试:

@Test
public void Test1() throws Exception { ... }

@Test
public void Test2() throws Exception { ... }

我希望 Test2 运行 Test1 之后 仅当 Test1 成功了。

如何在 TestNG 中实现这一点?

您可以像这样添加 @dependsOnMethods and alwaysRun 属性:

@Test
public void Test1() throws Exception { ... }

@Test(dependsOnMethods = {"Test1", ..., ...}, alwaysRun = false)
public void Test2() throws Exception { ... }

您还可以将测试设置为 @BeforeSuite,如果失败则抛出任何异常 - 所有挂起的测试都将被跳过。它也适用于 @BeforeMethod@BeforeTest@BeforeClass