英文:
Create after method for running only after several methods (UI,Java,TestNG)
问题
你好,请告诉我如何在多个方法之后仅运行一个方法?
我尝试在 Java + TestNG + Maven 的 UI 自动化测试中实现此功能。
提前感谢。
英文:
Hello tell me please how can I run after method only after several methods?
I try to create it for UI autotests with Java + TestNG + Maven.
Thanks in advance
答案1
得分: 1
看看这个简单的代码,这可能会对你解释 **`@AfterGroups`** 的工作原理有所帮助:
import org.testng.annotations.AfterGroups;
import org.testng.annotations.Test;
public class MyTest {
@Test(groups= {"group1"})
public void yourTest1() {
System.out.println("yourTest1");
}
@Test(groups= {"group1"})
public void yourTest2() {
System.out.println("yourTest2");
}
@Test
public void yourTest3() {
System.out.println("yourTest3");
}
@AfterGroups("group1")
public void tearDown() {
System.out.println("tearDown");
}
}
输出结果:
yourTest1
yourTest2
tearDown
yourTest3
对于处理多个组的 **`@AfterGroups`**,请用多个 `groups` 名称进行定义,像这样:
public class MyTest {
@Test(groups= {"group1"})
public void yourTest1() {
System.out.println("yourTest1");
}
@Test(groups= {"group2"})
public void yourTest2() {
System.out.println("yourTest2");
}
@Test
public void yourTest3() {
System.out.println("yourTest3");
}
@AfterGroups(groups= {"group1", "group2"})
public void tearDown() {
System.out.println("tearDown");
}
}
输出结果:
yourTest1
tearDown
yourTest2
tearDown
yourTest3
希望对你有所帮助。
英文:
Check it out this simple code, this might make a little clue to you how @AfterGroups
works:
import org.testng.annotations.AfterGroups;
import org.testng.annotations.Test;
public class MyTest {
@Test(groups= {"group1"})
public void yourTest1() {
System.out.println("yourTest1");
}
@Test(groups= {"group1"})
public void yourTest2() {
System.out.println("yourTest2");
}
@Test
public void yourTest3() {
System.out.println("yourTest3");
}
@AfterGroups("group1")
public void tearDown() {
System.out.println("tearDown");
}
}
Output:
yourTest1
yourTest2
tearDown
yourTest3
For @AfterGroups
which handles multiple groups, please define with multiple groups
name, looks like this:
public class MyTest {
@Test(groups= {"group1"})
public void yourTest1() {
System.out.println("yourTest1");
}
@Test(groups= {"group2"})
public void yourTest2() {
System.out.println("yourTest2");
}
@Test
public void yourTest3() {
System.out.println("yourTest3");
}
@AfterGroups(groups= {"group1", "group2"})
public void tearDown() {
System.out.println("tearDown");
}
}
Output:
yourTest1
tearDown
yourTest2
tearDown
yourTest3
Hope this helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论