星期日, 8月 20, 2006

TestNG備忘記

TestNG(Testing, the Next Generation) 測試框架項目靈感來自 JUnit 及 NUnit,
主要目的是增強舊的測試框架不足的功能, 其新增的功能如下:
- JDK 5 Annotations
- 新增測試設定檔 (預設 testng.xml)
- 提供 data-driven testing (利用 @DataProvider 將 data 提供給測試程式)
- 支持 parameters (可使用設定檔或 @DataProvider 提供參數功能)
- 提供分佈式測試.
- 提供功能強勁的執行模組 (不再使用 TestSuite)
- 給予多種工具支援 (Eclipse, IDEA, Maven, etc...)
- 嵌入 BeanShell
- 預設的執行時期 JDK 功能及日誌
- 給予應用程式提供相依的 methods

詳細的介紹請參閱以下官方文檔.
(注意, 官方的文檔有些新的 method 及 depricated method 還沒有修改)
http://testng.org/doc/documentation-main.html

開始備忘記:
[1] 安裝 jdk 5
[2] 安裝 Eclipse WTP
[3] 安裝 TestNG eclipse plugin
[4] 建立 Eclipse project

[1] 安裝 jdk 5:
下載 jdk-1_5_0_07-nb-5_0-win-ml.exe
http://java.sun.com/j2se/1.5.0/download-netbeans.html
安裝至 D:\jdk1.5.0_07
新增環境變數 JAVA_HOME=D:\jdk1.5.0_07
D:\jdk1.5.0_07\bin 加入至 PATH 中
D:\jdk1.5.0_07\lib\dt.jar 及 D:\jdk1.5.0_07\lib\tools.jar 加入至 CLASSPATH 中
執行 D:\>java -version
輸出 java version "1.5.0_07" 即安裝成功.

[2] 安裝 Eclipse WTP:
下載 wtp-all-in-one-sdk-R-1.5.0-200606281455-win32.zip
http://www.eclipse.org/webtools/
http://www.eclipse.org/downloads/download.php?file=/webtools/downloads/drops/R-1.5.0-200606281455/wtp-all-in-one-sdk-R-1.5.0-200606281455-win32.zip
解壓至 D:\eclipse_wtp

[3] 安裝 TestNG eclipse plugin:
Eclipse:Help -> Software Updates -> Find and Install -> Search for new features to install
按 New Remote Site
Name: TestNG
URL: http://beust.com/eclipse
然後安裝.

[4] 建立 Eclipse project:
Eclipse: File -> New -> Other -> Java Project
Project Name: First_TestNG -> Finish

打開 TestNG View:
Eclipse: Window -> Show View -> Other ->> Java -> TestNG
如下圖所示


下載 testng-5.0.2.zip
http://testng.org/doc/download.html
http://testng.org/testng-5.0.2.zip

解壓縮至 D:\testng-5.0.2
將 D:\testng-5.0.2\testng-5.0.2-jdk15.jar 加入到 First_TestNG project classpath 裡.

建立測試檔案:
/*------------------ TestNGTest.java -----------------------*/
package testng.joeyta;

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

public class TestNGTest {

@BeforeTest // 標記指定所有測試進行前呼叫此 method
public void beforeAllTest(){
System.out.println("Before All Test");
}

@BeforeMethod
// 標記指定每個測試進行前呼叫此 method
public void setUp() {
System.out.println("Before Each Test Method");
}

@Test(groups = { "group1" }) // 標記為測試程式, 並為分組 group1
public void group1Test() {
System.out.println("Group 1");
}

@Test(groups = { "group2" })
// 標記為測試程式, 並為分組 group2
public void group2Test() {
System.out.println("Group 2");
}

@AfterMethod
// 標記指定每個測試進行後呼叫此 method
public void tearDown(){
System.out.println("After Each Test Method");
}

@AfterTest
// 標記指定所有測試進行後呼叫此 method
public void afterAllTest(){
System.out.println("After All Test");
}
}
/*------------------ TestNGTest.java -----------------------*/


右鍵點選 TestNGTest.java -> Run As -> TestNG Test

Cosole 輸出為:
Before All Test
Before Each Test Method
Group 2
After Each Test Method
Before Each Test Method
Group 1
After Each Test Method
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\testng.joeyta.TestNGTest.html
PASSED: group2Test
PASSED: group1Test

===============================================
testng.joeyta.TestNGTest
Tests run: 2, Failures: 0, Skips: 0
===============================================

After All Test

===============================================
First_TestNG
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\toc.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\testng.joeyta.TestNGTest.properties
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\index.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\main.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\groups.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\methods.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\methods-alphabetical.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\classes.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\reporter-output.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\methods-not-run.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\testng.xml.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\index.html
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\testng-failed.xml
Creating D:\eclipse_wtp\workspace\First_TestNG\test-output\First_TestNG\testng-failed.xml


執行結果如下圖所示


項目結構如下所示


由於使用 TestNG eclipse plugin , 故不需要建立 testng xml configuration file.
它會自動產生. 並會產生相關 test output html 文檔.

內容如下
<!----------------- temp-testng-customsuite.xml -------------------->
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="First_TestNG">
<test verbose="2" name="testng.joeyta.TestNGTest" annotations="JDK5">
<classes>
<class name="testng.joeyta.TestNGTest"/>
</classes>
</test>
</suite>
<!----------------- temp-testng-customsuite.xml -------------------->


右鍵選擇 /test-output/index.html -> Open With -> Web Browser
如下圖所示


官方網
http://testng.org/

http://jira.opensymphony.com/browse/TESTNG

http://beust.com/weblog/

沒有留言: