两个注解都会在testNG中的@Test之前运行,那么它们之间有什么区别。
两个注解都会在testNG中的@Test之前运行,那么它们之间有什么区别。
请检查以下代码和输出
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Test_BeforeTestAndBeforeMethod {
@BeforeTest
public void beforeTest()
{
System.out.println("beforeTest");
}
@BeforeMethod
public void beforeMethod()
{
System.out.println("\nbeforeMethod");
}
@Test
public void firstTest()
{
System.out.println("firstTest");
}
@Test
public void secondTest()
{
System.out.println("secondTest");
}
@Test
public void thirdTest()
{
System.out.println("thirdTest");
}
}
输出:
beforeTest
beforeMethod
firstTest
beforeMethod
secondTest
beforeMethod
thirdTest
@BeforeTest : 在测试方法之前仅调用一次。
@BeforeMethod: 在每个测试方法之前每次都会调用。
示例:
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Test_BeforeTestAndBeforeMethod {
@BeforeTest
public void beforeTestDemo()
{
System.out.println("This is before test calling.");
}
@BeforeClass
public void beforeClassDemo()
{
System.out.println("This is before class calling.");
}
@BeforeMethod
public void beforeMethodDemo()
{
System.out.println("This is before method calling.");
}
@Test
public void testADemo()
{
System.out.println("This is Test1 calling.");
}
@Test
public void testBDemo()
{
System.out.println("This is Test2 calling.");
}
@Test
public void testCDemo()
{
System.out.println("This is Test3 calling.");
}
@AfterMethod
public void afterMethodDemo()
{
System.out.println("This is after method calling.");
}
@AfterClass
public void afterClassDemo()
{
System.out.println("This is after class calling.");
}
@AfterTest
public void afterTestDemo()
{
System.out.println("This is after test calling.");
}
}
@BeforeTest
:在任何测试方法之前只会被调用一次,无论有多少个方法使用了@Test
注解,它只会被调用一次。
@BeforeMethod
:在每个使用@Test
注解的方法之前都会被调用,如果你有10个@Test
方法,它将被调用10次。
要了解BeforeClass
和BeforeTest
之间的区别,请参考答案https://dev59.com/UF0a5IYBdhLWcg3wNWXA#57052272
我知道已经有几个很好的答案回答了这个问题,我只是想在此基础上将注释视觉化地与testng.xml中的xml元素联系起来,并包括before/after suite。
我尽量让它对新手友好,希望这可以帮助到你。
我的java示例基本上只是Ishita Shah代码的格式化版本。
BeforeAfterAnnotations.java(假设此文件位于名为“test”的包中)
package test;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BeforeAfterAnnotations
{
@BeforeSuite
public void beforeSuiteDemo()
{
System.out.println("\nThis is before a <suite> start tag.");
}
@BeforeTest
public void beforeTestDemo()
{
System.out.println("\tThis is before a <test> start tag.");
}
@BeforeClass
public void beforeClassDemo()
{
System.out.println("\t\tThis is before a <class> start tag.\n");
}
@BeforeMethod
public void beforeMethodDemo()
{
System.out.println("\t\t\tThis is before a method that is annotated by @Test.");
}
@Test
public void testADemo()
{
System.out.println("\t\t\t\tThis is the testADemo() method.");
}
@Test
public void testBDemo()
{
System.out.println("\t\t\t\tThis is the testBDemo() method.");
}
@Test
public void testCDemo()
{
System.out.println("\t\t\t\tThis is the testCDemo() method.");
}
@AfterMethod
public void afterMethodDemo()
{
System.out.println("\t\t\tThis is after a method that is annotated by @Test.\n");
}
@AfterClass
public void afterClassDemo()
{
System.out.println("\t\tThis is after a </class> end tag.");
}
@AfterTest
public void afterTestDemo()
{
System.out.println("\tThis is after a </test> end tag.");
}
@AfterSuite
public void afterSuiteDemo()
{
System.out.println("This is after a </suite> end tag.");
}
}
testng.xml(testng.xml --> 点击运行 --> TestNG Suite)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Before/After Annotations Suite">
<test name="Before/After Annotations Test">
<classes>
<class name="test.BeforeAfterAnnotations" />
</classes>
</test>
</suite>
Output to console
[RemoteTestNG] detected TestNG version 7.0.0
This is before a <suite> start tag.
This is before a <test> start tag.
This is before a <class> start tag.
This is before a method that is annotated by @Test.
This is the testADemo() method.
This is after a method that is annotated by @Test.
This is before a method that is annotated by @Test.
This is the testBDemo() method.
This is after a method that is annotated by @Test.
This is before a method that is annotated by @Test.
This is the testCDemo() method.
This is after a method that is annotated by @Test.
This is after a </class> end tag.
This is after a </test> end tag.
This is after a </suite> end tag.
===============================================
Before/After Annotations Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
@BeforeTest
- 在testng.xml中声明的每个测试之前运行
@BeforeMethod
- 在类内部声明的每个带有@Test
注释的测试方法之前运行
在TestNG中
@BeforeMethod - BeforeMethod 在每个测试方法之前执行。所有使用@Test注释的方法。@BeforeMethod适用于Java类中定义的测试。
@BeforeTest - BeforeTest 仅在testng.xml文件中给定的标签之前执行。@BeforeTest适用于在testng.xml中定义的测试。
参考:- https://examples.javacodegeeks.com/enterprise-java/testng/testng-beforetest-example/ 和http://howtesting.blogspot.com/2012/12/difference-between-beforetest-and.html
@BeforeTest
会在注入任何bean之前执行。与之相反的是,@BeforeMethod
在bean注入之后执行。不确定为什么会设计成这样。@BeforeTest
只会在所有测试方法之前执行一次。这些方法将在执行任何标有 @Test
注解的测试方法之前运行,这些测试方法是 testNG.xml 文件中 <test>
标签的一部分。
@BeforeMethod
将在每个标有 @Test
注解的方法之前执行。
@BeforeTest 在执行 testng.xml 文件中 < test > 标签中包含的任何测试方法之前执行设置方法。 @BeforeMethod 在执行标记为 @Test 的任何测试方法之前执行设置方法。