-2

如何演示在 TestNG Selenium中使用BeforeTest, AfterTest, BeforeSuite, AfterSuite, BeforeClass, AfterClass,注释的实时示例。BeforeMethodAfterMethod

4

1 回答 1

0

为了演示通过BeforeTest使用, AfterTest, BeforeSuite, AfterSuite, BeforeClass,AfterClass 注释的实时示例,您不需要 Selenium。

在IDE中安装TestNG插件后,您只需:

  • 提及方法的注释。例子:

    • @BeforeSuite
    • @BeforeClass
    • @BeforeMethod
    • @BeforeTest
    • @Test
    • @AfterTest
    • @AfterMethod
    • @AfterClass
    • @AfterSuite
  • 为类添加相关导入。

    • import org.testng.annotations.BeforeSuite;
    • import org.testng.annotations.BeforeClass;
    • import org.testng.annotations.BeforeMethod;
    • import org.testng.annotations.BeforeTest;
    • import org.testng.annotations.Test;
    • import org.testng.annotations.AfterTest;
    • import org.testng.annotations.AfterMethod;
    • import org.testng.annotations.AfterClass;
    • import org.testng.annotations.AfterSuite;
  • 示例代码块:

    package demo;
    
    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 TestNG_Annotation_Demo {
    
        @BeforeSuite
        public void before_suite()
        {
            System.out.println("I am in BeforeSuite");
        }
    
        @BeforeClass
        public void before_class()
        {
            System.out.println("I am in BeforeClass");
        }
    
        @BeforeMethod
        public void before_method()
        {
            System.out.println("I am in BeforeMethod");
        }
    
        @BeforeTest
        public void before_test()
        {
            System.out.println("I am in BeforeTest");
        }
    
        @Test
        public void test()
        {
            System.out.println("I am in Test");
        }
    
        @AfterTest
        public void after_test()
        {
            System.out.println("I am in AfterTest");
        }
    
        @AfterMethod
        public void after_method()
        {
            System.out.println("I am in AfterMethod");
        }
    
        @AfterClass
        public void after_class()
        {
            System.out.println("I am in AfterClass");
        }
    
        @AfterSuite
        public void after_suite()
        {
            System.out.println("I am in AfterSuite");
        }
    }
    
  • 控制台输出:

    [RemoteTestNG] detected TestNG version 6.14.2
    I am in BeforeSuite
    I am in BeforeTest
    I am in BeforeClass
    I am in BeforeMethod
    I am in Test
    I am in AfterMethod
    I am in AfterClass
    I am in AfterTest
    PASSED: test
    
    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
    I am in AfterSuite
    
    ===============================================
    Default suite
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
于 2019-08-29T15:00:05.340 回答