创建一个仅在多个方法(UI、Java、TestNG)之后运行的方法。

huangapple go评论68阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年6月5日 20:44:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62215666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定