如何演示在 TestNG 中使用 BeforeTest、AfterTest、BeforeSuite、AfterSuite、BeforeClass、AfterClass 注释的实时示例 [英] How to demonstrate a real time example of using BeforeTest, AfterTest, BeforeSuite, AfterSuite, BeforeClass, AfterClass annotations in TestNG

查看:31
本文介绍了如何演示在 TestNG 中使用 BeforeTest、AfterTest、BeforeSuite、AfterSuite、BeforeClass、AfterClass 注释的实时示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何演示使用BeforeTestAfterTestBeforeSuiteAfterSuite的实时示例TestNG Selenium 中的 BeforeClassAfterClassBeforeMethodAfterMethod 注释.

How to demonstrate real time example of using BeforeTest, AfterTest, BeforeSuite, AfterSuite, BeforeClass, AfterClass, BeforeMethod, AfterMethod annotations in TestNG Selenium.

推荐答案

用于演示使用 BeforeTestAfterTestBeforeSuite 的实时示例>、AfterSuiteBeforeClassAfterClass 注解 你不需要 Selenium.

For demonstrating a real time example of using BeforeTest, AfterTest, BeforeSuite, AfterSuite, BeforeClass, AfterClass annotations through testng you don't need Selenium.

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

Once you install the TestNG plugin within your IDE you simply need to:

  • 提及方法的注释.示例:

  • Mention the annotations for the methods. Example:

  • @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");
    }
}

  • 控制台输出:

  • Console Output:

    [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
    ===============================================
    

  • 这篇关于如何演示在 TestNG 中使用 BeforeTest、AfterTest、BeforeSuite、AfterSuite、BeforeClass、AfterClass 注释的实时示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆