[英]@BeforeClass Method not runing before each class
I have two classes and each class contains 2 test cases and Test1 class having one method with @BeforeClass as per me this method should run before Test2 class too but it is not running.我有两个类,每个 class 包含 2 个测试用例和 Test1 class 有一个方法与 @BeforeClass 根据我这个方法应该在 Test2 class 之前运行,但它没有运行。
package WebPackage;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Test1 {
@BeforeClass
public void test1() {
System.out.println("printing Before Class Method");
}
@Test (priority = 1)
public void test2() {
System.out.println("printing test_2");
}
@Test (priority = 3)
public void test3() {
System.out.println("printing test_3");
}
}
Test2测试2
package WebPackage;
import org.testng.annotations.Test;
public class Test2 {
@Test (priority = 1)
public void test4() {
System.out.println("printing test_4");
}
@Test (priority = 3)
public void test5() {
System.out.println("printing test_5");
}
}
Xml file Xml 文件
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Menu">
<test name="WebPackage">
<classes>
<class name="WebPackage.Test1"/>
<class name="WebPackage.Test2"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Console安慰
[RemoteTestNG] detected TestNG version 7.0.0
printing Before Class Method
printing test_2
printing test_3
printing test_4
printing test_5
===============================================
Menu
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
BeforeClass
runs only once when Test class is started.当测试 class 启动时,
BeforeClass
只运行一次。 Hence.因此。 it executes only once per test class.
每个测试 class 只执行一次。 Use
@Before
if you want to execute it with every test method in test class.如果你想用测试
@Before
中的每个测试方法执行它,请使用 @Before。
Read ref - Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll阅读参考文献 - @Before、@BeforeClass、@BeforeEach 和 @BeforeAll 之间的区别
@BeforeClass
will run only once before running any method annotated with @Test
in that class. @BeforeClass
将只运行一次,然后在 class 中运行任何带有@Test
注释的方法。 It will not run again for any method in your Test2
class.对于您的
Test2
class 中的任何方法,它将不会再次运行。
You can make a BaseTest
class with @BeforeClass
annotation and then each Test class extend with BaseTest.您可以使用
@BeforeClass
注释制作BaseTest
class,然后使用 BaseTest 扩展每个测试 class。
BaseTest:基础测试:
public class BaseTest {
@BeforeClass
public void test1() {
System.out.println("printing Before Class Method");
}
}
Test1:测试1:
public class Test1 extends BaseTest {
@Test (priority = 1)
public void test2() {
System.out.println("printing test_2");
}
@Test(priority = 3)
public void test3() {
System.out.println("printing test_3");
}
}
Test2:测试2:
public class Test2 extends BaseTest {
@Test(priority = 1)
public void test4() {
System.out.println("printing test_4");
}
@Test (priority = 3)
public void test5() {
System.out.println("printing test_5");
}
}
Output: Output:
printing Before Class Method
printing test_2
printing test_3
printing Before Class Method
printing test_4
printing test_5
===============================================
Menu
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.