TestNG是什麼
到這裡,大家肯定已經執行了一些關於Selenium的測試用例了(這裡主要是站著一個自動化測試工程師的角度來看待TestNG的,所以這裡所說的也都是基於Web測試的驗證點來說的,而不是針對單元測試來說。這裡就涉及到了Selenium),也產生了相應的格式的測試輸出結果了。現在我們將學習一下怎樣利用TestNG這個測試框架將測試結果報告顯示出來。
TestNG和Junit一樣都是一種測試框架,不過它克服了Junit存在的一些侷限性。NG意味著"Next Generation".大部分的Selenium測試工程師都選擇TestNG而非Junit就是因為TestNG的優勢。在TestNG中出現了更多的功能相對於Junit來說,但是在這裡我們只關注在Selenium測試中應用比較多的功能。
TestNG VS Junit
TestNG相對於JUnit有三點主要的優勢:
- 註釋更容易理解
- 測試用例能以組的概念出現會顯得更簡單
- 並行測試成為可能
Installing TestNG in Eclipse
步驟1
-
Launch Eclipse.
-
On the menu bar, click Help.
-
Choose the "Install New Software..." option.
Step 2
In the Install dialog box, click the Add button
Step 3
-
In "Name", type TestNG.
-
In "Location", type http://beust.com/eclipse.
-
Click OK
Step 4
-
Notice that "TestNG - http://beust.com/eclipse" was populated onto the "Work with:" textbox.
-
Check the "TestNG" check box as shown below, then click Next.
-
Note: In the latest Eclipse (Kepler) you don't have a checkbox for testNG, instead you click on question mark (help) icon which will open up the form, and you can select all and installation will continue as for the remaining instructions. Thanks Jana for the tip!
Step 5
-
Click Next again on the succeeding dialog box until you reach the License Agreement dialog.
-
Click "I accept the terms of the license agreement" then click Finish.
Step 6
Wait for the installation to finish
If you encounter a Security warning, just click OK
Step 7
When Eclipse prompts you for a restart, just click Yes.
Step 8
After restart, verify if TestNG was indeed successfully installed. Click Window > Preferences and see if TestNG is included on the Preferences list.
Step9
We will now start to import the TestNG Libraries onto our project. Click on the "Libraries" tab, and then "Add Library…"
Step 10
On the Add Library dialog, choose "TestNG" and click Next.
Step 11
Click Finish.
You should notice that TestNG is included on the Libraries list.
Step 12
We will now add the JAR files that contain the Selenium API. These files are found in the Java client driver that we downloaded from http://docs.seleniumhq.org/download/ when we were installing Selenium and Eclipse in the previous chapters.