TestNG - 設定注釈の例

TestNG –構成アノテーションの例

testng-configuration

TestNGでは、次の注釈を使用して、データベースのセットアップ/クリーンアップ、ダミーデータの準備、サーバーのデプロイ/シャットダウンなど、テストクラスの構成を行うことができます。

@BeforeSuite - For suite test, run before all tests in this suite have run.
@AfterSuite -  For suite test, run after all tests in this suite have run.

@BeforeTest - For suite test, run before any test method belonging to the classes inside the  tag is run.
@AfterTest - For suite test, run after all the test methods belonging to the classes inside the  tag have run.

@BeforeGroups: Run before the first test method that belongs to the group is invoked.
@AfterGroups: Run after the last test method that belongs to the groups is invoked.

@BeforeClass - Run before the first test method in the current class is invoked.
@AfterClass - Run after all the test methods in the current class have been run.

@BeforeMethod - Run before each test method.
@AfterMethod - Run after each test method.

P.S Suite test – Run multiple test classes together.

次の例を確認して、実行順序を確認します。最初に呼び出されるメソッドと次に実行されるメソッドです。

1. シングルテストクラス

単一のテストケースを実行して、groupclass、およびmethodの前後の使用法を示します。

TestConfiguration.java

package com.example.testng.examples.configuration;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestConfiguration {

    @BeforeGroups("shopping")
    public void beforeGroups() {
        System.out.println("@BeforeGroups");
    }

    @AfterGroups("shopping")
    public void afterGroups() {
        System.out.println("@AfterGroups");
    }

    @BeforeClass
    public void beforeClass() {
        System.out.println("@BeforeClass");
    }

    @AfterClass
    public void afterClass() {
        System.out.println("@AfterClass");
    }

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("@BeforeMethod");
    }

    @AfterMethod
    public void afterMethod() {
        System.out.println("@AfterMethod");
    }

    @Test(groups = "shopping")
    public void runTest1() {
        System.out.println("@Test - runTest1");
    }

    @Test
    public void runTest2() {
        System.out.println("@Test - runTest2");
    }
}

出力

@BeforeClass

@BeforeGroups
@BeforeMethod
@Test - runTest1
@AfterMethod
@AfterGroups

@BeforeMethod
@Test - runTest2
@AfterMethod

@AfterClass

PASSED: runTest1
PASSED: runTest2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

2. スイートテストクラス

suitetestの前後の使用を示すために、さらに2つのテストクラスを作成します。

DBConfig.java

package com.example.testng.examples.configuration;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

public class DBConfig {

    @BeforeSuite()
    public void beforeSuite() {
        System.out.println("@BeforeSuite");
    }

    @AfterSuite()
    public void afterSuite() {
        System.out.println("@AfterSuite");
    }

    @BeforeTest()
    public void beforeTest() {
        System.out.println("@BeforeTest");
    }

    @AfterTest()
    public void afterTest() {
        System.out.println("@AfterTest");
    }

}

TestDBConnection.java

package com.example.testng.examples.configuration;

import org.testng.annotations.Test;

public class TestDBConnection {

    @Test
    public void runOtherTest1() {
        System.out.println("@Test - runOtherTest1");
    }

    @Test
    public void runOtherTest2() {
        System.out.println("@Test - runOtherTest2");
    }

}

複数のテストケースを一緒に実行するXMLファイルを作成します。 わかりやすいXMLコメントを読んでください。

testng.xml






    
    
      
        
        
        
      
    
    

    
    
      
        
        
      
    
    


出力

@BeforeSuite

@BeforeTest     //Start {case1}
@BeforeClass
@BeforeGroups
@BeforeMethod
@Test - runTest1
@AfterMethod
@AfterGroups
@BeforeMethod
@Test - runTest2
@AfterMethod
@AfterClass
@Test - runOtherTest1
@Test - runOtherTest2
@AfterTest      //End {case1}


@BeforeTest     //Start {case2}
@Test - runOtherTest1
@Test - runOtherTest2
@AfterTest      //End {case2}

@AfterSuite

===============================================
TestAll
Total tests run: 6, Failures: 0, Skips: 0
===============================================

完了しました。