如果在TestNG中执行组,则不会调用@Before Suite和@BeforeTest方法

naq*_*ash 2 testng annotations

下面是我的XML文件和Demo Class.Precondition()方法将在demo1()方法之前运行,而postCondition()方法将在demo1()方法之后运行.同样的过程适用于demo2().但是当我运行代码时,不会调用BeforeSuite和BeforeTest方法.为什么.?怎么称呼他们?

Output :           
Before Method is executing                                                       
DEMO -1   
After Method is executing  
Before Method is executing  
DEMO 2  
After Method is executing
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Test">
        <groups>
            <run>
                <include name = "Hey"></include>
            </run>
        </groups>
        <classes>
            <class name="practicepackage.Demo"/>
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->  
Run Code Online (Sandbox Code Playgroud)
package practicepackage;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Demo {

    @BeforeSuite
    public void beforeSuite(){
        System.out.println("Before Suite method is being launched");
    }

    @BeforeTest
    public void beforeTest(){
        System.out.println("Before Test Method is being luanched");
    }

    @BeforeMethod(groups = {"Hey"})
    public void PreCondition(){
        System.out.println("Before Method is executing");
    }

    @Test (groups = {"Hey"})
    public void demo1(){
        System.out.println("DEMO -1 ");
    }

    @Test(groups = {"Hey"})
    public void demo2(){
        System.out.println("DEMO 2");
    }

    @AfterMethod(groups = {"Hey"})
    public void postCondition(){
        System.out.println("After Method is executing");
    }
}
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Test">
        <groups>
            <run>
                <include name = "Hey"></include>
            </run>
        </groups>
        <classes>
            <class name="practicepackage.Demo"/>
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->
Run Code Online (Sandbox Code Playgroud)

Kri*_*van 6

为了确保始终执行@BeforeSuite@BeforeTest执行,请启用alwaysRun=true这些注释的属性.

这是必需的,因为当您遍历组时,TestNG不会选择这些配置方法,除非它们是您选择的组的一部分.

TestNG中的组选择可以被视为TestNG中的一种过滤机制,它可以让您在决定运行哪些测试时告诉TestNG过滤的标准.