java TestNG注解顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28591382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TestNG Annotations order
提问by zmorris
I have a question about the order of operations with TestNG annotations... I have the following code:
我对 TestNG 注释的操作顺序有疑问......我有以下代码:
public class AnnotationsTest {
@BeforeSuite(alwaysRun = true)
public static void beforeSuite() {
System.out.println("@BeforeSuite");
}
@BeforeClass(alwaysRun = true)
public static void beforeClass() {
System.out.println("@BeforeClass");
}
@BeforeTest(alwaysRun = true)
public static void beforeTest() {
System.out.println("@BeforeTest");
}
@BeforeMethod(alwaysRun = true)
public static void beforeMethod() {
System.out.println("@BeforeMethod");
}
@AfterSuite(alwaysRun = true)
public static void afterSuite() {
System.out.println("@AfterSuite");
}
@AfterClass(alwaysRun = true)
public static void afterClass() {
System.out.println("@AfterClass");
}
@AfterTest(alwaysRun = true)
public static void afterTest() {
System.out.println("@AfterTest");
}
@AfterMethod(alwaysRun = true)
public static void afterMethod() {
System.out.println("@AfterMethod");
}
@Test
public void test() {
System.out.println("Test");
}
@Test
public void test2() {
System.out.println("Test2");
}
}
My output is the following:
我的输出如下:
My question is, why is @AfterTest method not run after each @Test annotation? Does TestNG treat the entire class as the 'Test'? It seems like this is the case because the @BeforeTest and @AfterTest are outside of the @BeforeClass and @AfterClass, but I wanted to be sure I understand. I assume I could use the @BeforeMethod and @AfterMethod in this case to execute before and after the test1 and test2 in this class. Thanks!
我的问题是,为什么 @AfterTest 方法不在每个 @Test 注释之后运行?TestNG 是否将整个班级视为“测试”?似乎是这种情况,因为@BeforeTest 和@AfterTest 在@BeforeClass 和@AfterClass 之外,但我想确保我理解。我假设在这种情况下我可以使用 @BeforeMethod 和 @AfterMethod 在这个类中的 test1 和 test2 之前和之后执行。谢谢!
采纳答案by Neeraj Jain
My question is, why is @AfterTest method not run after each @Test annotation?
我的问题是,为什么 @AfterTest 方法不在每个 @Test 注释之后运行?
As the Documentationsays
正如文档所说
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
@AfterTest:注解的方法将在属于 < test> 标签内的类的所有测试方法运行后运行。
and
和
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@BeforeTest:注释的方法将在任何属于 < test> 标签内的类的测试方法运行之前运行。
回答by Niteen Sugre
The main confusion here is with @Test annotation and tag. Consider that you are executing above code from testng.xml file. And the way we write testng file tag sequence in that file is methods . So it now each annotation will make sense. i.e. @Test is used for methods within that class. @BeforeMethod will get execute before each @Test annotation. And @BeforeTest annotation execute before classes mentioned in testng.xml file as you can see tag encloses tag. So short anwser for above question is @Test is used for menthod @BeforeMethod is executed before each @Test annoted method and @BeforeTest is executed before all methods and classes mentioned in tag.
这里的主要混淆是@Test 注释和标签。考虑到您正在从 testng.xml 文件执行上述代码。我们在该文件中编写 testng 文件标记序列的方式是 methods 。所以现在每个注释都有意义了。即@Test 用于该类中的方法。@BeforeMethod 将在每个 @Test 注释之前执行。@BeforeTest 注释在 testng.xml 文件中提到的类之前执行,你可以看到标签包含标签。对于上述问题,简短的回答是 @Test 用于在每个 @Test 注释方法之前执行 @BeforeMethod 并且 @BeforeTest 在标记中提到的所有方法和类之前执行。