Testng-execution-procedure
提供:Dev Guides
TestNG-実行手順
この章では、TestNGのメソッドの実行手順について説明します。 呼び出されるメソッドの順序を説明します。 次に、TestNGテストAPIメソッドの実行手順と例を示します。
*C:\> TestNG_WORKSPACE* にJavaクラスファイル名 *TestngAnnotation.java* を作成して、注釈をテストします。
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class TestngAnnotation {
//test case 1
@Test
public void testCase1() {
System.out.println("in test case 1");
}
//test case 2
@Test
public void testCase2() {
System.out.println("in test case 2");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("in beforeMethod");
}
@AfterMethod
public void afterMethod() {
System.out.println("in afterMethod");
}
@BeforeClass
public void beforeClass() {
System.out.println("in beforeClass");
}
@AfterClass
public void afterClass() {
System.out.println("in afterClass");
}
@BeforeTest
public void beforeTest() {
System.out.println("in beforeTest");
}
@AfterTest
public void afterTest() {
System.out.println("in afterTest");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("in beforeSuite");
}
@AfterSuite
public void afterSuite() {
System.out.println("in afterSuite");
}
}
次に、 C:\> TestNG_WORKSPACE に testng.xml ファイルを作成して、注釈を実行します。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Suite1">
<test name = "test1">
<classes>
<class name = "TestngAnnotation"/>
</classes>
</test>
</suite>
javacを使用してテストケースクラスをコンパイルします。
C:\TestNG_WORKSPACE>javac TestngAnnotation.java
ここで、指定されたテストケースクラスで定義されたテストケースを実行するtestng.xmlを実行します。
C:\TestNG_WORKSPACE>java org.testng.TestNG testng.xml
出力を確認します。
in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
上記の出力に基づいて、実行手順は次のとおりです-
- まず、beforeSuite()メソッドは1回だけ実行されます。
- 最後に、afterSuite()メソッドは1回だけ実行されます。
- メソッドbeforeTest()、beforeClass()、afterClass()、およびafterTest()のメソッドも一度だけ実行されます。
- beforeMethod()メソッドは、各テストケースに対して実行されますが、テストケースを実行する前に実行されます。
- afterMethod()メソッドは、各テストケースに対して実行されますが、テストケースの実行後に実行されます。
- beforeMethod()とafterMethod()の間で、各テストケースが実行されます。