【发布时间】:2018-11-21 12:46:19
【问题描述】:
两个注解都在 testNG 中的 @test 之前运行,那么它们之间的区别是什么。
【问题讨论】:
标签: testng
两个注解都在 testNG 中的 @test 之前运行,那么它们之间的区别是什么。
【问题讨论】:
标签: testng
检查下面的代码并输出
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 :它将在Test方法之前调用仅一次。
@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.");
}
}
【讨论】:
This is Test2 calling -> This is Test3 calling.
@BeforeTest : 对任何测试方法只调用一次,不管有多少个方法用@Test注解,都只调用一次
@BeforeMethod 它会在每个带有@Test 注释的方法之前被调用,如果你有10 个@Test 方法,它将被调用10 次
想知道BeforeClass和BeforeTest有什么区别,请参考答案https://stackoverflow.com/a/57052272/1973933
【讨论】:
我知道这个问题已经有好几个很好的答案了,我只是想在它们的基础上构建以直观地将注释与 testng.xml 中的 xml 元素联系起来,并包括之前/之后的套件。
我尽量让它对新手友好,我希望它有所帮助。
我的 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>
输出到控制台
[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
===============================================
【讨论】:
在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- 在 testng.xml 中声明的每个测试之前运行
@BeforeMethod- 在类中声明的每个测试方法之前和@Test 注释下运行
【讨论】:
@BeforeTest 在运行集成测试时在注入任何 bean 之前执行。与 @BeforeMethod 形成对比,后者在 bean 注入后执行。不知道为什么会这样设计。
【讨论】:
@BeforeTest 只会在任何测试方法之前执行一次。方法将在执行任何 @Test 注释测试方法之前运行,该方法是 testNG.xml 文件中 <test> 标记的一部分。
@BeforeMethod 将在每个使用 @Test 注释的方法之前执行。
【讨论】:
@BeforeTest
在 testng.xml 文件的
【讨论】: